Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Feature request: should_request expectation

35 views
Skip to first unread message

Evgeniy Dolzhenko

unread,
Feb 23, 2011, 1:45:19 PM2/23/11
to webmock-users
Hi all,

First of all - thanks a lot to all you guys who developed
this great tool!

Now I have a simple feature request, in RSpec we could
write spec code like this:

it 'should fetch page' do
Net::HTTP.should_receive(:get_response).with('http://example.com')
fetch('http://example.com') # or anything else which fetches the
page
end

now if my understanding is correct with WebMock we'd
have to do something like:

it 'should fetch page' do
stub_request(:any, "http://example.com")

fetch('http://example.com')

a_request(:get, "http://example.com").should_not have_been_made
end

now I would like to write this instead:

it 'should fetch page' do
WebMock.should_request(:get, "http://example.com")
fetch('http://example.com')
end

i.e. set up stub and expectation in the same line,
is that a reasonable feature to have? Or is it possible
to do that already?

Sincerely,
Evgeniy

Thibaut Barrère

unread,
Nov 22, 2011, 8:22:37 AM11/22/11
to webmoc...@googlegroups.com
Hello!

I have exactly the same question but could not yet figure this out (I'm only getting started with WebMock): how to stub a request and put an expectation at the same time? Is it currently possible using a built-in function in WebMock, or should I just use two statements or a quick helper of mine?

Thanks for any hint,

-- Thibaut

Evgeniy Dolzhenko

unread,
Nov 22, 2011, 8:30:57 AM11/22/11
to webmoc...@googlegroups.com
We're using something like

stub_request(:any, //)

do_work

a_request(:get, "http://example.com/").should have_been_made


HTH,
Evgeniy

Bartosz Blimke

unread,
Nov 22, 2011, 8:35:44 AM11/22/11
to webmoc...@googlegroups.com
Webmock by design follows stub-execute-verify paradigm and it separates stubs
from expectations. Each of them can be used independently.

You can however set expectations on already created stub if you want to avoid duplication.

stub = stub_request(:get, "www.example.com")
# ... make requests ...
stub.should have_been_requested

Bartosz



2011/11/22 Thibaut Barrère <thibaut...@gmail.com>

Evgeniy Dolzhenko

unread,
Nov 22, 2011, 8:37:06 AM11/22/11
to webmoc...@googlegroups.com
Hey, that's handy, thanks!

On 11/22/2011 5:35 PM, Bartosz Blimke wrote:
> Webmock by design follows stub-execute-verify paradigm and it separates
> stubs
> from expectations. Each of them can be used independently.
>
> You can however set expectations on already created stub if you want to
> avoid duplication.
>

> stub = stub_request(:get, "www.example.com <http://www.example.com>")


> # ... make requests ...
> stub.should have_been_requested
>
> Bartosz
>
>
>

> 2011/11/22 Thibaut Barr�re <thibaut...@gmail.com
> <mailto:thibaut...@gmail.com>>

Reply all
Reply to author
Forward
0 new messages