webmock and cucumber

384 views
Skip to first unread message

Florian Rosenberg

unread,
Feb 24, 2012, 10:25:50 AM2/24/12
to webmock-users
Hi,

i'm having some troubles getting Webmock to work with Cucumber. I've
been using webmock with RSpec successfully and I think it is a great
framework. Now, I have some cucumber scenarios need some request
stubbing. However, for some reason, webmock doesn't "do" anything or I
may have missed something.

I have the following setup (cucumber 1.1.9 and webmock 1.8)

support/env.rb

require 'webmock/cucumber'

also tried (did not make a difference)
World(WebMock::API, WebMock::Matchers)
WebMock.disable_net_connect!

I my steps file has this:

Given /^I want to deploy a new system "([^"]*)"$/ do |arg1|

# read json files
creation_request_json = File.read(File.join(File.dirname(__FILE__),
'../../data/json_files/creation_request.json'))
creation_response_json = File.read(File.join(File.dirname(__FILE__),
'../../data/json_files/creation_response.json'))
url = '...'

stub_request(:post, url).
with(:body => creation_request_json,
:headers => {'Accept'=>'application/json',
'Accept-Language'=>'en',
'Authorization'=>'Basic ZGV2T3BzMTpkZXZPcHMx',
'Content-Type'=>'application/json'}).
to_return(:status => 201, :body => creation_response_json, :headers
=> {})

# call out to new system

end

I'm using the same code successfully in Rspec. I invoke cucumber via
rake with the following definition:

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |task|
task.cucumber_opts = ["test/features"]
end

Any pointers appreciated.

Bartosz Blimke

unread,
Feb 24, 2012, 10:33:03 AM2/24/12
to webmoc...@googlegroups.com
What do you mean by Webmock not doing anything?

Are you getting errors or is webmock not intercepting any requests at all?

What http client lib are you using?

It's difficult to say without having a code to reproduce the problem.

Bartosz

2012/2/24 Florian Rosenberg <f.ros...@gmail.com>

Florian Rosenberg

unread,
Feb 24, 2012, 10:40:22 AM2/24/12
to webmoc...@googlegroups.com
I was not very clear, sorry. I mean I don't see my requests be intercepted. I use the rest-client gem, so I presume standard Ruby HTTP libs.

In RSpec, I saw all my non-intercepted requests being printed out to the console with a suggested stubbing code. This is also not happening with Cucumber, I see all my requests going out to the actual REST resources. I'm using the same stubbing code as in my "old" RSpect tests, but I have no luck.

Is there any debug options I can try in cucumber? 

--Florian

Bartosz Blimke

unread,
Feb 24, 2012, 10:45:11 AM2/24/12
to webmoc...@googlegroups.com
Weird. If WebMock is required it should reject all unstubbed requests by default.

WebMock is enabled in the process cucumber is running right? Is it possible
you are making these requests from a separate process/server without webmock?

Florian Rosenberg

unread,
Feb 24, 2012, 11:36:26 AM2/24/12
to webmoc...@googlegroups.com
Ouch, I guess I found the problem. I'm testing a CLI tool with cucumber and it run it in a separate process, that is why Webmock is not intercepting. Currently, I'm calling my cmd line took (based on Thor) like this:

system("#{CLI_TOOL} deploy #{@description} -c #{@cloud_id} -p #{@properties}")

The CLI tools calls out via the REST client and I would like to stub these request. Is there a way to make that happen? 

Regards,
-Florian

Bartosz Blimke

unread,
Feb 24, 2012, 12:20:10 PM2/24/12
to webmoc...@googlegroups.com
I would test these two processes separately.

In your cucumber I'd just ensure you are correctly invoking the other process with "system".

Then I would test the other process separately using Webmock.
Reply all
Reply to author
Forward
0 new messages