Skip to content

Commit

Permalink
Only remove request_stub if it's present
Browse files Browse the repository at this point in the history
* Fixes issue when calling `with_mounted` within Rspec since Webmock has an after hook on Rspec to always reset the mocks
  • Loading branch information
greis committed May 22, 2015
1 parent b5d952b commit 94b6493
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/mock5.rb
Expand Up @@ -66,7 +66,9 @@ def mount(*apis)
def unmount(*apis)
mounted_apis.intersection(apis).each do |api|
mounted_apis.delete api
registry.remove_request_stub api.request_stub
if registry.request_stubs.include?(api.request_stub)
registry.remove_request_stub api.request_stub
end
end
end

Expand Down
28 changes: 21 additions & 7 deletions spec/mock5_spec.rb
Expand Up @@ -16,7 +16,6 @@

describe "API mgmt" do
before do
WebMock::StubRegistry.instance.reset!
described_class.instance_exec do
if instance_variable_defined?(:@_mounted_apis)
remove_instance_variable :@_mounted_apis
Expand Down Expand Up @@ -182,13 +181,28 @@ def post(url, params={})
end
end

before{ described_class.mount api, another_api }
context "#mount" do
before{ described_class.mount api, another_api }

it "stubs remote apis" do
expect(get("http://example.com/index.html?foo=bar")).to eq("index.html")
expect(post("http://example.com/submit/here?foo=bar")).to eq("submit")
expect(post("http://example.com/foo/bar?fizz=buzz")).to eq("bar")
expect(get("http://example.com/bar/foo")).to eq("foo")
it "stubs remote apis" do
expect(get("http://example.com/index.html?foo=bar")).to eq("index.html")
expect(post("http://example.com/submit/here?foo=bar")).to eq("submit")
expect(post("http://example.com/foo/bar?fizz=buzz")).to eq("bar")
expect(get("http://example.com/bar/foo")).to eq("foo")
end
end

context "#with_mounted" do
around do |example|
described_class.with_mounted api, another_api, &example
end

it "stubs remote apis" do
expect(get("http://example.com/index.html?foo=bar")).to eq("index.html")
expect(post("http://example.com/submit/here?foo=bar")).to eq("submit")
expect(post("http://example.com/foo/bar?fizz=buzz")).to eq("bar")
expect(get("http://example.com/bar/foo")).to eq("foo")
end
end
end
end
Expand Down

0 comments on commit 94b6493

Please sign in to comment.