Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

added a method to remove stubs #211

wants to merge 3 commits into


None yet
3 participants

I needed to remove some specific stubs in one project and was unable to find this feature. If there's something wrong, please, let me know, so I can fix it ASAP.


bblimke commented Sep 12, 2012

Hi Douglas,

Thanks for this pull request.
Since you are first one who requested this feature in WebMock, could you please describe why do you need to remove stubs in your project?

I'm not sure if comparing to_s is the way to go.
You can possibly have multiple stubs with the same uri and method, but different to_s result due to query, body or headers.
Maybe best way would be if remove_stub used already implemented match behaviour.

I would also use remove_request_stub instead of remove_stub to avoid possible conflicts and make it obvious it's about requests and not any other stubs.

Hi, thanks for the fast answer. I need this for the fake client to a storage web service that provides a uri to download stored files. The client gem should be able to be used in tests even without a real server running and we decided that the best way to do this is implementing a fake client instead of a fake server.

So, the problem is: this fake client is being used inside a Rails app and I need to fake the download of an already stored file using the fake client, but I don't have a real service or any fake web server running. Then, I need to stub a uri to return the file. That's ok. But the gem has a method to update files too, then I'd need to remove the old stub and stub the same uri again, but to return another body.

It might be a little confusing, did you understand?

Sure I can use the implemented match behaviour and change the method name, I made it in a rush and didn't have time to look more deeply for a better way to compare the stubs.

Excuse me, but how I use, or where's the implemented match behaviour?


bblimke commented Sep 17, 2012

There is only matching behaviour between RequestStub (RequestPattern) and RequestSignature.

Did it!


bblimke commented Sep 29, 2012

Thanks. Much better not to depend on to_s method.

In this case RequestSignature works as a matcher and RequestPattern works as a matched object.

I.e remove_request_stub(:get, "www.example.com") will match both stub_request(:get, /example/) and stub_request(:get, "www.example.com"). Is that desired?

It won't make any difference for me. But if it does for you, I think I can fix it too.


bblimke commented Oct 1, 2012

For me id doesn't make any difference, since I don't need this feature, but I hope it's usefult to others :)
I just want to make sure it's the best approach, before adding it officially to webmock api.

Another option would be to use object identity (stub_request returns RequestStub) object,
so this can be solved in many different ways :)

any chance to get that merged?


bblimke commented Dec 7, 2012

@jmatraszek could you please explain how do you want to use this feature?

This would help to understand what's the best way to include it in webmock's api.


bblimke commented Feb 17, 2013

@jmatraszek @douglascamata Is this pull request still valid?

@jmatraszek I would like to get some feedback on how this feature is going to be used before adding it to webmock api.

Webmock always uses the last declared stub matching given request, so is there a need to remove old stubs from the stack?

Hi @bblimke,
sorry I didn't respond for such a long time -- I found a workaround and forgot about this issue.

I have a Device model. Every time I create device instance I issue a request to check if I am allowed to create that device. In specs I stub this request with success for most of my specs. There are also some specs in which I want to stub this request with failure. I do not want to break the rest of specs (they should not fail on creating device). In that case I cannot delete the last stub (failure), but I have to create another one with success. So instead of having only one stub in webmock's stack, I am having three of them.

That's not the big deal (and I have a workaround for it already), but it would be nice to be able to keep only one stub.

@bblimke I'm not quite sure about what we did in project about this issue, it ended a few months ago. But I think we implemented a fake webserver for the tests.


bblimke commented Feb 28, 2013

@jmatraszek I added remove_request_stub method to the api (not released yet). It accepts stub object as an argument.

@bblimke bblimke closed this Feb 28, 2013

thanks a lot! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment