Trouble with RSpec undefined method `request=' for API

1,527 views
Skip to first unread message

Glenn Goodrich

unread,
May 15, 2014, 4:10:03 PM5/15/14
to ruby-...@googlegroups.com
I am trying to get RSpec testing to work.  I (think I) have followed the instructions in the readme, but I keep getting:

 1) API::V1::Accounts GET :id returns an account by some_id
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `request=' for #<API::V1::Accounts:0x007fb67afd3710>
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/actionpack-3.2.18/lib/action_controller/test_case.rb:507:in `setup_controller_request_and_response'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-rails-2.14.2/lib/rspec/rails/adapters.rb:112:in `block (2 levels) in setup'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:21:in `run'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `block in run'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `each'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `run'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:345:in `run_before_each_hooks'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:294:in `run_before_each'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:113:in `block in run'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-rails-2.14.2/lib/rspec/rails/example/controller_example_group.rb:159:in `call'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-rails-2.14.2/lib/rspec/rails/example/controller_example_group.rb:159:in `block (2 levels) in <module:ControllerExampleGroup>'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/ggoodrich/.rvm/gems/ruby-2.0.0-p247@kyck_auth_provider/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'


for a simple test.

Here's my api:

module API
  module V1
    class Accounts < Grape::API
      include Defaults

      resource :accounts do
        desc "Return an account by ID"
        params do
          requires :id, type: String, desc: "The ID to retrieve"
        end
        route_param :id do
          get do
            puts 'HI'
            authenticated?
            a = Account.where(some_id: params[:id]).first
            present a, with: API::V1::Accounts::Entity
          end
        end
...

Here's my spec:

require 'spec_helper'


describe API::V1::Accounts do

  describe "GET :id" do
    let!(:account) do
      Account.create!(
        first_name: 'Test',
        last_name:'Tester',
        email: 'te...@test.com',
        password: '123456',
        password_confirmation: '123456'
      )
    end
    it "returns an account by id" do
      puts account.id
      get "/api/v1/accounts/#{account.id}"
      puts 'after'
      response.body.should == account.to_json
    end
  end
end

I never see 'HI' puts'ed

What am I doing wrong?

Thanks,
Glenn

Daniel Doubrovkine

unread,
May 15, 2014, 4:43:24 PM5/15/14
to ruby-...@googlegroups.com
Looks like RSpec thinks this is a Rails controller test. It should be a "request" type.  I think this should be helpful: http://stackoverflow.com/questions/6233079/rspec-2-config-type-types

cheers
dB. 


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Glenn Goodrich

unread,
May 15, 2014, 5:07:45 PM5/15/14
to ruby-...@googlegroups.com
Well, that got rid of the error, but it never gets into the route handler.  So, I never see 'HI'

Any idea why it's not getting in there? I gets in the param blocks, just never executes the route handler.

Thanks,
Glenn

Glenn Goodrich

unread,
May 15, 2014, 5:34:27 PM5/15/14
to ruby-...@googlegroups.com
More info, response.status == 301, so, it's redirecting, but response.body is empty

FWIW, if I go to the same path in the browser, it works.



On Thursday, May 15, 2014 4:10:03 PM UTC-4, Glenn Goodrich wrote:

Glenn Goodrich

unread,
May 15, 2014, 5:40:13 PM5/15/14
to ruby-...@googlegroups.com
RESULT!

So, I had config.force_ssl = true in my application.rb.  Changing this to false in environments/test.rb solved my issue.

Yay!

On Thursday, May 15, 2014 4:10:03 PM UTC-4, Glenn Goodrich wrote:
Reply all
Reply to author
Forward
0 new messages