jdliss / shoulda-callback-matchers

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

Intermittent failures #14

Closed SirRawlins closed 9 years ago

SirRawlins commented 9 years ago

For some reason which I'm as yet unable to trace I'm getting intermittent failures in my tests which asset for the existence of a callback.

So for instance I have a spec which looks something like this:

    describe "callbacks" do

        it "defined a callback to add itself to the router" do
            # Expect the callback.
            expect(subject).to callback(:enable_route).after(:create)
        end

        it "defined a callback to remove itself from the router" do
            # Expect the callback.
            expect(subject).to callback(:disable_route).after(:destroy)
        end         

        it "defined a callback to update the routes if exired flag changes" do
            # Expect the callback.
            expect(subject).to callback(:update_route).after(:update).if(:expired_changed?)
        end         

    end

Most of the time these specs will pass just fine, however on occasion they will fail, with the following error.

 Failure/Error: expect(subject).to callback(:update_route).after(:update).if(:expired_changed?)
   expected update_route to be listed as a callback after update if expired_changed? evaluates to true, but was not

This seems to happen when running suites of specs rather than individual specs on their own, if that makes much difference to the situation?

This is using version 1.1.1, rspec 2.14 and rails 4.1

Any suggestions or ideas as to what might be causing this?

beatrichartz commented 9 years ago

Sounds to me like a spec is leaking state somewhere - the matchers in your case examine the return value of the private method _update_callbacks on your model and then do some assertions on them. In your case, maybe putting a debugger into the statement here https://github.com/beatrichartz/shoulda-callback-matchers/blob/master/lib/shoulda/callback/matchers/active_model.rb#L67 and evaluating the assertions one by one will prove helpful.

Better failure messages with indications of partial failure would be helpful as well, I raised an issue for that.

Feel free to reopen if this proves to be a bug in the library.

SirRawlins commented 9 years ago

@beatrichartz thanks for the suggestions. I'll take a closer looks at some point and try and pin down what's going on.