-
-
Save cemeng/1122488 to your computer and use it in GitHub Desktop.
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 |
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: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'
"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.
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:
- 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'
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.
Can you check what Rack::Utils.build_nested_query( params ) prduces? Is "completed_at" not part of the produced string?
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 :)
Happy to help :) Good luck with the other error.
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.