Created
November 7, 2009 05:43
-
-
Save nmk/228556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# recent github checkout | |
$:.unshift(File.dirname(__FILE__) + '/lib/mongomapper/lib/') | |
require 'mongo_mapper' | |
require 'spec' | |
MongoMapper.database = 'tmp' | |
class Party | |
include MongoMapper::Document | |
many :guests | |
def invite(name) | |
if guests.any? { |g| g.name == name } | |
errors.add(:guests, 'duplicate name') # after this the errors stay empty | |
else | |
guests << Guest.new(:name => name) | |
end | |
end | |
end | |
class Guest | |
include MongoMapper::EmbeddedDocument | |
key :name, :required => true | |
end | |
context 'Party' do | |
before(:each){ Party.collection.remove } | |
it 'is boring without guests' do | |
party = Party.create | |
party.invite('George') | |
party.should be_valid | |
party.guests.length.should == 1 | |
end | |
it 'leads to confusion if guests are named the same' do | |
party = Party.create | |
party.invite('George') | |
party.invite('Melinda') | |
party.invite('George') | |
party.guests.length.should == 2 # this is ok | |
party.should_not be_valid # this fails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment