Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to use basic stub_request in spec?

2,796 views
Skip to first unread message

Vell

unread,
Dec 8, 2014, 8:33:57 AM12/8/14
to webmoc...@googlegroups.com
Hello all,

I am attempting to use a basic stub_request to simulate an error 500 when making a web request. Unfortunately I was not able to understand how to get this to work. I was wondering if anyone can tell me what I am missing.

My current code I have for the moment is:

context 'an invalid connection' do
      it
'should return an error message in error array' do
        stub_request
(:any, "http://api.openweathermap.org/").to_return(:status => [500, "Internal Server Error"])
     
end
   
end

The error I am getting is:

Failures:


 
1) OpenWeatherMapWrapper#current_weather an invalid connection should return an error message in error array
     
Failure/Error: stub_request(:any, "http://api.openweathermap.org/").to_return(:status => [500, "Internal Server Error"])
     
NoMethodError:
       
undefined method `stub_request' for #<RSpec::ExampleGroups::OpenWeatherMapWrapper::CurrentWeather::AnInvalidConnection:0x007fa92c430418>
     # ./spec/apis/open_weather_map_wrapper_spec.rb:22:in `
block (4 levels) in <top (required)>

I tried adding 

require 'webmock/rspec'

But that didn't seem to address the issue.

The end result is that I would be able to stub the request to the root url and then expect to get the error 500 for the test to pass.

Any help is greatly appreciated.

Thanks,

Bartosz Blimke

unread,
Dec 8, 2014, 9:40:31 AM12/8/14
to webmoc...@googlegroups.com
Have you added 'require 'webmock/rspec'' to spec_helper.rb?

Does it work if you call WebMock.stub_request ?

Bartosz

--

---
You received this message because you are subscribed to the Google Groups "webmock-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webmock-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vell

unread,
Dec 8, 2014, 10:33:53 AM12/8/14
to webmoc...@googlegroups.com
Thanks for the response Bartosz.

I actually added it in the test itself as an initial test. My initial assumption was that because I had webmock required in my gem file that I really didn't think I needed to add it anywhere else. So I just added it in the specific test that I had planned to use it with and see if it would work then move it to my rails_helper.rb file if it worked. Thats how I got the error.

So I added the require 'webmock/rspec' to the rails helper and ran the test and I didn't get any errors. So I think I am set there.

Now I just need to figure out how to complete the rest of my test :)
Reply all
Reply to author
Forward
0 new messages