Skip to content

Instantly share code, notes, and snippets.

@cemeng
Created August 3, 2011 12:10
Show Gist options
  • Save cemeng/1122488 to your computer and use it in GitHub Desktop.
Save cemeng/1122488 to your computer and use it in GitHub Desktop.
unit test w/ webmock
require 'helper'
class TestDailymileRuby < Test::Unit::TestCase
def setup
WebMock.disable_net_connect!
end
should "be able to post a simple entry" do
# Stub setup
params = {
:message => "Totally awesome",
:oauth_token => "XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4",
:completed_at => "2011-07-19 13:00"
}
stub_request(:post, "https://api.dailymile.com/entries.json").
with(
:body => Rack::Utils.build_nested_query( params ),
:headers => {
'Accept'=>'application/json',
'Authorization'=>'OAuth XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4',
'Content-Type'=>'application/x-www-form-urlencoded',
'User-Agent'=>'dailymile-ruby/0.2.0'}
).
to_return(:status => 200, :body => "", :headers => {}
)
# Test
Dailymile::Client.set_client_credentials 'qnFYExIvAhpsBLY4DU7w4jJerrmtBTUOQ4zccS1e', 'uE2tbUVPAjzXftUVZySLGvISEQkyeGQrSuh3Jz1n'
client = Dailymile::Client.new 'XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4'
entry = Dailymile::Entry.new :message => "Totally awesome"
client.post_entry(entry.entry)
end
end
@bblimke
Copy link

bblimke commented Aug 3, 2011

So what's not working exactly? :)

Test looks ok but I have no idea what Dailymile::Client is or what http library does it use.

@cemeng
Copy link
Author

cemeng commented Aug 3, 2011

I've received this error when running rake test:
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: POST

I believe it's using faraday

I've got a felling I am missing something here..
Here's a stack trace:
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/webmock-1.6.4/lib/webmock/http_lib_adapters/net_http.rb:72:in request_with_webmock' /Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/adapter/net_http.rb:45:incall'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/request.rb:85:in run' /Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/request.rb:27:inrun'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/connection.rb:177:in run_request' /Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/oauth2-0.1.1/lib/oauth2/client.rb:63:inrequest'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/oauth2-0.1.1/lib/oauth2/access_token.rb:28:in `request'

@bblimke
Copy link

bblimke commented Aug 3, 2011

"Real HTTP connections are disabled. Unregistered request: POST" is that all?
WebMock should print the unregistered request details as well as stubbing instructions. Please compare the difference
between your stub declaration and request details.

@cemeng
Copy link
Author

cemeng commented Aug 3, 2011

Ah no - that's not all - sorry should've given you more details.

I've compared the stub and the request details they look identical to me.

Here is the complete details:

  1. Error:
    test: DailymileRuby should be able to post a simple entry. (TestDailymileRuby):
    WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: POST https://api.dailymile.com/entries.json with body 'message=Totally+awesome&oauth_token=XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4' with headers {'Accept'=>'application/json', 'Authorization'=>'OAuth XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'dailymile-ruby/0.2.0'}

You can stub this request with the following snippet:

stub_request(:post, "https://api.dailymile.com/entries.json").
with(:body => "message=Totally+awesome&oauth_token=XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4",
:headers => {'Accept'=>'application/json', 'Authorization'=>'OAuth XXVTcIrfN2cIs43Yg3de56LIYGsWjTXmNiRmR6H4', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'dailymile-ruby/0.2.0'}).
to_return(:status => 200, :body => "", :headers => {})

/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/webmock-1.6.4/lib/webmock/http_lib_adapters/net_http.rb:72:in `request_with_webmock'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/adapter/net_http.rb:45:in `call'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/request.rb:85:in `run'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/request.rb:27:in `run'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/faraday-0.5.7/lib/faraday/connection.rb:177:in `run_request'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/oauth2-0.1.1/lib/oauth2/client.rb:63:in `request'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/oauth2-0.1.1/lib/oauth2/access_token.rb:28:in `request'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/connection/token.rb:18:in `block in request'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/connection.rb:37:in `call'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/connection.rb:37:in `make_request'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/connection/token.rb:17:in `request'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/connection.rb:16:in `post'
/Users/cemeng/prj/dailymile-ruby/lib/dailymile/client.rb:64:in `post_entry'
test/test_dailymile-ruby.rb:33:in `block in <class:TestDailymileRuby>'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
/Users/cemeng/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

@cemeng
Copy link
Author

cemeng commented Aug 3, 2011

I then tried to use the stub request from WebMock snippet - but I got different error, JSON parse error.
But this is not unrelated to WebMock.

Perhaps I should go down this path - and work out why the stub snippet doesn't work.

@bblimke
Copy link

bblimke commented Aug 3, 2011

Can you check what Rack::Utils.build_nested_query( params ) prduces? Is "completed_at" not part of the produced string?

@cemeng
Copy link
Author

cemeng commented Aug 3, 2011

You are right! I didn't set the completed_at for the actual API call that I was testing - but I set it up on stub_request.

So I removed the completed_at from the stub_request - and I got different error - but the error is totally unrelated to WebMock.

Thanks for helping me debugging this :)

@bblimke
Copy link

bblimke commented Aug 3, 2011

Happy to help :) Good luck with the other error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment