The stack trace of the failure points to line 30 of lib/api_client.rb, but
there aren't 30 lines in what you pasted of that file - following the stack
trace should help you pin it down.

Also you should try to replicate the problem directly in the console (test
environment), so if just doing this causes the problem, it's nothing
specific with your spec, but may be something specific with the test
environment (is the AppSettings class behavior properly in the test
environment?):

> ApiClient.get_user_by_email('[email protected]')

Finally, when you get this working, you can certainly use webmock or
flexmock directly, but it might help to make use of VCR which wraps some
common stuff you might be doing up quite nicely:

https://github.com/myronmarston/vcr

Ben


On Tue, Dec 27, 2011 at 5:21 PM, Benjamin Wanicur <[email protected]>wrote:

> Hi Everyone
>
> I have spinning my wheels for awhile with this problem.  I am hoping  a
> more experienced TDD dev out there might be able to help.
>
> I am testing an api client that consumes an internal api that handles user
> registration / auth for several client applications.
>
>
> I have my api_client.rb file, which lives in lib/api_client.rb:
> class ApiClient
>   include HTTParty
>
>   protocol = (Rails.env.production? ? 'https' : 'http')
>   base_uri
> "#{protocol}://#{AppSettings[:api][:hostname]}:#{AppSettings[:api][:port]}"
>   default_params :token => AppSettings[:api][:api_key]
>   @@ext = '.json'
>
>   def self.get_user_by_email(email, all_atts=false)
>     data = get("/users/by_email#{@@ext}", :query => {:email => email,
> :all_atts => all_atts}).parsed_response
>   end
>
> end
>
>
> And here is the rspec file, which lives here: spec/lib/api_client_spec.rb
> require 'spec_helper'
> require 'webmock/rspec'
>
> describe ApiClient do
>
>   it "should be able to detect if a user exists in the API" do
>     # stub_request(:get,
> "#{API_URL}/users/by_email.json?all_atts=true&email=
> [email protected]&token=#{API_KEY}").to_return(:status => 200,
> :body => '')
>     res = ApiClient.get_user_by_email('[email protected]')
>     # res.should == {}
>   end
>
> end
>
>
> As you can see in the spec file, I was planning on using webmock (
> https://github.com/bblimke/webmock) to mock the API responses.  However,
> things are breaking before that even happens.  I have commented out the
> stub_request line, and I still get no warning/error message from webmock.
>  Here is the output from my test:
>
>
> ... bundle exec rspec spec/lib/api_client_spec.rb --format=d
>
> ApiClient
>   should be able to detect if a user exists in the API (FAILED - 1)
>
> Failures:
>
>   1) ApiClient should be able to detect if a user exists in the API
>      Failure/Error: res = ApiClient.get_user_by_email('
> [email protected]')
>      NoMethodError:
>        undefined method `info' for nil:NilClass
>      # ./lib/api_client.rb:30:in `get_user_by_email'
>      # ./spec/lib/api_client_spec.rb:25:in `block (2 levels) in <top
> (required)>'
>
> Finished in 17.37 seconds
> 1 example, 1 failure
>
> Failed examples:
>
> rspec ./spec/lib/api_client_spec.rb:23 # ApiClient should be able to
> detect if a user exists in the API
>
>
> I included the information about the webmock-ing *just in case*.  It seems
> like the problem is happening before the ApiClient can even make its
> request.  Any help is much appreciated!
>
> PS - google groups will not let you post an email address inside a post.
>  the test email addresses have been hidden.
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to