rspec-mocks
rspec-mocks copied to clipboard
Mocks appear to persist across context in rspec-mocks v 3.3.2
I have a test setup that runs using the database cleaner style of RSpec tests, keeping tests atomic. The problem with this is that some of my models use the after_commit callback which doesn't get thrown in my test environment. I get around this by manually calling the callback in certain tests. The tests themselves run a Sidekiq job that is used and tested in different areas. The tests work fine when I don't mock out that Sidekiq worker. When I mock that Sidekiq worker (in an entirely different file and context) the tests break saying that the API that should have been called by the worker (mocked using WebMock) has not been called. I'm not sure, but I think this is a bug in rspec-mocks.
Obviously this is a bit of a complex setup so let me know what information I can provide to help debug the issue.
WebMock mocks are not rspec-mocks and are not directly tied in to the rspec example life cycle. I believe you have to tie them in yourself so that they don't leak between tests.
You need to call WebMock.reset!
after each example, this can be done with a configuration hook or by requiring webmock/rspec
which will do this for you.
Thanks guys! I will try this later and double check. Hopefully that's all the problem was. I didn't realize WebMock leaked like that.
It's not leaking it just needs to be setup correctly.
WebMock is a generic tool that can be used in any test framework and outside any test framework, and thus it assumes nothing about your environment and you need to configure it to work how you want.
Thanks for the support guys! It appears even after adding in the require statement that the problem persists in my other tests.
You'll have to seek support from WebMock's maintainer then. If you see that behavior from an rspec mock we'd certainly dig in more.
I appreciate it. I will certainly post in the WebMock issue tracker. The odd part is that the thing that causes the tests to fail is just adding in additional tests that have rspec-mock mocks setup in the individual tests within their own separate context. I think the issue may be separate from WebMocks due to this. I have a couple of objects that have async workers (using Sidekiq) that I mock some methods on in the tests that cause the breaking change. These methods also are the ones that make some API calls in the tests that have WebMock in them in another file. It seems that the mock is persisting outside of the scope of the context these are setup in. I'm not 100% sure though, as I have said the only thing I know 100% for sure is that putting the tests in breaks the suite and removing them fixes it. Very strange.
@illegalnumbers did you get this issue worked out?
Closing during the monorepo migration due a lack of a reproduction for anything we can fix.