3

What is the easiest way to test if HTTP-request is not made?

Say, I want my application NOT to send an API call to a remote server, if the user doesn't supply a username.

What RSpec test should I write in my Rails app to test that HTTP-request is never made? Can fakeweb help me somehow?

Alex Smolov
  • 1,831
  • 4
  • 24
  • 43

3 Answers3

5

You should probably try WebMock.
It allows you to check the number of times some url was requested.

assert_requested :post, "http://www.example.com", :times => 0
Ivan Zamylin
  • 1,708
  • 2
  • 19
  • 35
1

Say we have the following code that makes a conditional request to GitHub's API.

if condition
  user = Octokit.user 'dideler'
end

Using RSpec, you can test that the object doesn't receive the message for the method call.

expect(Octokit).to_not receive(:user)

If it passes, that means the request wasn't made.

Dennis
  • 56,821
  • 26
  • 143
  • 139
0

I think I understand what you are asking. You just want to error check to see if a request sent or valid.

If username != blank --> api call else return 'you did not input a username'.

Or you can use the api's return data to check if the request was valid.

I hope this helps.

recneps
  • 1,285
  • 5
  • 16
  • 27
  • Sorry, I need to make my question more clear. What RSpec test should I write in my Rails app to test that HTTP-request is never made? I've updated my question as well. – Alex Smolov Mar 18 '13 at 15:36