ruby on rails - Rspec should_receive has me stumped -
i need test whether instance method gets called on particular instance result of calling class method. like:
class dog < activerecord::base def self.roll_trained dog.all.each { |d| d.rollover if d.trained? } end def rollover # rollover , stuff end def trained? self.trained == true end end
i've written test like:
describe 'dog.roll_trained' 'rolls trained dogs' dog_1 = dog.create(trained: true) dog_1.should_receive(:rollover) dog.roll_trained end end
i thought had right, test fails. doing wrong here?
the problem here dog_1 , instances looped on dog.each not same instance. because activerecord generates different instance same row (dog_1) in database when dog.each
i have written blog post on can read here: activerecord , in-memory object state
one solution stub dog.all - dog.stub(all: [dog_1])
another solution save trained
attribute database , spec against database: dog_1.reload.trained.should be_true
Comments
Post a Comment