jdliss / shoulda-callback-matchers

Test existence of your Rails callbacks without having to call them
MIT License
134 stars 18 forks source link

Testing callback conditions #5

Closed aaronchi closed 10 years ago

aaronchi commented 10 years ago

Should matchers don't currently take into account if/unless conditions on callbacks. Is there any way to include conditions in the tests or do these have to be tested separately?

beatrichartz commented 10 years ago

This is already supported, but got left out in the readme, thanks for letting me know!

Basically, you just use if and unless as chained methods, like so:

it { should callback(:assign_something).before(:create).if(:this_is_true) }

BartlomiejSkwira commented 9 years ago

Ok, so I have a model MyModel with an attribute my_attr and a callback:

after_update :do_whatever, if: :my_attr

How do I test that? This doesn't work:

should callback(:do_whatever).after(:update).if(self.subject.my_attr) #Test::Unit

#result of running this test
=> ... class:MyModelTest>': undefined method `my_attr' for nil:NilClass (NoMethodError)