require 'spec_helper' require 'fedux_org/stdlib/models' describe Models::BaseModel do before(:each) { Models::BaseModel.clear } it "is possible to register a message" do m1 = Models::BaseModel.new('name') Models::BaseModel.register m1 end context "#find" do it "is possible to find an object" do m1 = Models::BaseModel.new('name') Models::BaseModel.register m1 m_found = Models::BaseModel.find('name') expect(m1).to eq(m_found) end it "returns nil if no instance is available" do m_found = Models::BaseModel.find('name') expect(m_found).to eq(nil) end end context "#enable" do it "enables instances" do m = Models::BaseModel.create(:name1) Models::BaseModel.enable :name1 expect(Models::BaseModel.all.first).to eq(m) end it "raises an error if instance given by name is not available" do expect { Models::BaseModel.enable :name1 }.to raise_error FeduxOrg::Stdlib::Models::Exceptions::InvalidSearchCriteria end end it "returns a string of all active instances" do Models::BaseModel.create(:name1) Models::BaseModel.create(:name2) Models::BaseModel.enable :name1 Models::BaseModel.enable :name2 result = Models::BaseModel.all_names_as_string(", ") expect(result).to eq("name1, name2") result = Models::BaseModel.all_names_as_string(": ") expect(result).to eq("name1: name2") result = Models::BaseModel.all_names_as_string expect(result).to eq("name1, name2") end it "is possible to create and register in one step" do m1 = Models::BaseModel.create('name') m_found = Models::BaseModel.find('name') expect(m1).to eq(m_found) end it "is possible to delete an object" do m1 = Models::BaseModel.create('name') m2 = Models::BaseModel.delete('name') expect(m1).to eq(m2) end it "is possible to delete all objects" do Models::BaseModel.create('name1') Models::BaseModel.create('name2') Models::BaseModel.create('name3') Models::BaseModel.clear expect(Models::BaseModel.all).to eq( [] ) end it "return first object" do expected_result = Models::BaseModel.create('name1') Models::BaseModel.create('name2') Models::BaseModel.create('name3') result = Models::BaseModel.first expect(result).to eq( expected_result ) end it "return last object" do Models::BaseModel.create('name1') Models::BaseModel.create('name2') expected_result = Models::BaseModel.create('name3') result = Models::BaseModel.last expect(result).to eq( expected_result ) end end