Skip to content

Instantly share code, notes, and snippets.

@ashmoran
Created August 3, 2010 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashmoran/507140 to your computer and use it in GitHub Desktop.
Save ashmoran/507140 to your computer and use it in GitHub Desktop.
contract "[Entity] Collection:" do |collection_name, item_name, class_name|
let(:item_class) { qualified_const_get(class_name) }
let(:collection_member_1) { mock(item_class, id: 1, update: :unexpected_not_nil) }
let(:collection_member_2) { mock(item_class, id: 2, update: :unexpected_not_nil) }
let(:collection_member_3) { mock(item_class, id: 3, update: :unexpected_not_nil) }
# TODO: Decide how to get the Representations
let(:collection_member_representation) { mock("#{class_name}::Representation") }
before(:each) do
item_class.stub(:new).and_return(
collection_member_1, collection_member_2, collection_member_3
)
end
# def entity_dot_new_collection_member(*args)
# entity.send(:"new_#{item_name}", *args)
# end
#
# def entity_dot_get_collection_member(*args)
# entity.send(:"get_#{item_name}", *args)
# end
#
# def entity_dot_update_collection_member(*args)
# entity.send(:"update_#{item_name}", *args)
# end
#
# def entity_dot_delete_collection_member(*args)
# entity.send(:"delete_#{item_name}", *args)
# end
#
# def entity_dot_collection_members(*args)
# entity.send(collection_name, *args)
# end
define_method :entity_dot_new_collection_member do |*args|
entity.send(:"new_#{item_name}", *args)
end
define_method :entity_dot_get_collection_member do |*args|
entity.send(:"get_#{item_name}", *args)
end
define_method :entity_dot_update_collection_member do |*args|
entity.send(:"update_#{item_name}", *args)
end
define_method :entity_dot_delete_collection_member do |*args|
entity.send(:"delete_#{item_name}", *args)
end
define_method :entity_dot_collection_members do |*args|
entity.send(collection_name, *args)
end
define_method :entity_dot_collection_member_representations do |*args|
entity.send(:"#{item_name}_representations", *args)
end
describe "#{collection_name}" do
describe "Helper methods:" do
describe "#new_#{item_name}, #get_#{item_name}" do
it "creates a new #{class_name}" do
item_class.should_receive(:new).with(collection_member_representation)
entity_dot_new_collection_member(collection_member_representation)
end
it "returns the #{class_name}" do
entity_dot_new_collection_member(collection_member_representation).should eq collection_member_1
end
it "lets you get the #{class_name} back" do
new_collection_member = entity_dot_new_collection_member(collection_member_representation)
entity_dot_get_collection_member(new_collection_member.id).should eq collection_member_1
end
# TODO: fix described_class
it "adds a new #{class_name} to the <described_class>" do
entity_dot_new_collection_member(:unimportant_collection_member_representation)
entity_dot_new_collection_member(:unimportant_collection_member_representation)
entity_dot_new_collection_member(:unimportant_collection_member_representation)
# NOTE: This is deliberately loose. We need to investigate overriding/tightening contract on implenting classes.
entity_dot_collection_members.should =~ [collection_member_1, collection_member_2, collection_member_3]
end
end
describe "#update_#{item_name}" do
before(:each) do
entity_dot_new_collection_member(:unimportant_collection_member_representation)
entity_dot_new_collection_member(:unimportant_collection_member_representation)
end
it "updates the appropriate #{class_name}" do
collection_member_2.should_receive(:update).with(collection_member_representation)
entity_dot_update_collection_member(2, collection_member_representation)
end
it "returns nil" do
entity_dot_update_collection_member(2, collection_member_representation).should be_nil
end
end
describe "#delete_#{item_name}" do
before(:each) do
entity_dot_new_collection_member(:unimportant_collection_member_representation)
entity_dot_new_collection_member(:unimportant_collection_member_representation)
end
it "deletes the appropriate #{class_name}" do
entity_dot_delete_collection_member(2)
entity_dot_collection_members.should eq [ collection_member_1 ]
end
it "returns nil" do
entity_dot_delete_collection_member(2).should be_nil
end
end
end
describe "Representations:" do
let(:collection_member_c_representation) { mock(item_class::Representation, name: "C") }
let(:collection_member_a_representation) { mock(item_class::Representation, name: "A") }
let(:collection_member_b_representation) { mock(item_class::Representation, name: "B") }
before(:each) do
collection_member_1.stub(to_view_object: collection_member_c_representation)
collection_member_2.stub(to_view_object: collection_member_a_representation)
collection_member_3.stub(to_view_object: collection_member_b_representation)
end
it "returns unsorted #{class_name} representations" do
3.times { entity_dot_new_collection_member(:unimportant_collection_member_representation) }
entity_dot_collection_member_representations.should =~ [
collection_member_a_representation, collection_member_b_representation, collection_member_c_representation
]
end
end
end
end
module DomainLib
describe Entity do
# NOTE: This is the same Child class as above, see here for details:
# http://groups.google.com/group/rspec/browse_thread/thread/8ffd0dd43ee6560e
class Child
# TODO: Decide how to make the Representation class (fully) configurable
class Representation; end
end
let(:entity_class) {
Class.new do
extend Entity::ClassExtensions
include Entity::InstanceExtensions
collection :children, of: :child, class_name: Child.name
collection :minions, of: :minion, class_name: Child.name
end
}
let(:entity) { entity_class.new }
it_satisfies_contract "[Entity] Collection:", :children, :child, Child.name
it_satisfies_contract "[Entity] Collection:", :minions, :minion, Child.name
end
end
% rspec -fs spec/domain_lib/entity_spec.rb
DomainLib::Entity
satisfies contract: [Entity] Collection:
children
Helper methods:
#new_child, #get_child
creates a new DomainLib::Child
returns the DomainLib::Child
lets you get the DomainLib::Child back
adds a new DomainLib::Child to the <described_class>
#update_child
updates the appropriate DomainLib::Child
returns nil
#delete_child
deletes the appropriate DomainLib::Child
returns nil
Representations:
returns unsorted DomainLib::Child representations
satisfies contract: [Entity] Collection:
minions
Helper methods:
#new_minion, #get_minion
creates a new DomainLib::Child
returns the DomainLib::Child
lets you get the DomainLib::Child back
adds a new DomainLib::Child to the <described_class>
#update_minion
updates the appropriate DomainLib::Child
returns nil
#delete_minion
deletes the appropriate DomainLib::Child
returns nil
Representations:
returns unsorted DomainLib::Child representations
Finished in 0.19501 seconds
37 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment