ruby on rails - Testing if an instance of a class receives a message when a class method that sends the message to all instances is called -
though correct, title needs explanation :)
i have class:
class character include datamapper::resource def self.tick_all all.collect &:tick end def tick # stuff end end
as can see when character.tick_all
called instances should receive tick
invocation. works exptected: when fire console stuff in tick
gets done. can't tests pass:
describe ".tick_all" let(:instance) { factorygirl.create(:character) } "invokes #tick every instance" character.tick_all instance.should_receive(:tick) end end
failed example:
failure/error: instance.should_receive(:tick) (#<character:0x00000002fa4e28>).tick(any args) expected: 1 time received: 0 times
the expectation should set before method call:
describe ".tick_all" let(:instance) { factorygirl.create(:character) } "invokes #tick every instance" instance.should_receive(:tick) character.tick_all end end
upd: code above doesn't work. variant?
describe ".tick_all" "invokes #tick every instance" character.any_instance.should_receive(:tick) character.tick_all end end
upd2: aaand 1 more version:
describe ".tick_all" "invokes #tick every instance" tick_count = 0 character.any_instance.stub(:tick) { tick_count += 1 } character.tick_all tick_count.should == character.count # or # expect{ character.tick_all }.to change{tick_count}.by(character.count) end end
Comments
Post a Comment