Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing that HTTP-request is not made in Rails

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?

like image 620
Alex Smolov Avatar asked Nov 01 '25 08:11

Alex Smolov


2 Answers

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
like image 188
Ivan Zamylin Avatar answered Nov 03 '25 22:11

Ivan Zamylin


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.

like image 27
Dennis Avatar answered Nov 03 '25 22:11

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!