开发者

Rspec + Capybara : undefined last_response

开发者 https://www.devze.com 2023-03-26 06:31 出处:网络
I\'m setting up my test environment with Rspec + Capybara, but I get this undefined last_response. I was searching throught the web and SO. I found couple of things about version, use git repo instead

I'm setting up my test environment with Rspec + Capybara, but I get this undefined last_response. I was searching throught the web and SO. I found couple of things about version, use git repo instead of rubygem source etc ... But it does not change a clue for me.

Here the backtrace : rspec --backtrace spec/controllers/api/plist/providers_listing_spec.rb Failures:

1) Api::Plist::ProvidersController should return a successfull plist containing a list of providers
 Failure/Error: last_response.status.shoud be_success
 NameError:
   undefined local variable or method `last_response' for #<RSpec::Core::ExampleGroup::Nested_1:0x105658e10>
 # ./.gems/ruby/1.8/gems/rspec-expectations-2.6.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'
 # ./.gems/ruby/1.8/gems/actionpack-3.0.5/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
 # ./spec/controllers/api/plist/providers_listing_spec.rb:19
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `run'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `run_examples'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `run'
 # /Library/Ruby/Gems/1.8/gems/r开发者_Python百科spec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `run'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
 # /Library/Ruby/Gems/1.8/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'`

Here is my gem config for testing :

gem 'ruby-prof' # perf
gem 'rspec-rails', :git => 'git://github.com/rspec/rspec-rails.git'
gem 'shoulda' # Shoulda
gem 'factory_girl_rails' #=> mocking
gem 'capybara', :git => "http://github.com/jnicklas/capybara.git" 
gem 'database_cleaner'

gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i #guard mac dependency

gem 'guard-livereload' # browser reloading
gem 'guard-rspec'
gem 'guard-test'
gem 'growl' #grow notification

I'm using database_clearner to deal with mongo db ...

And untill now I don't really use :

require 'spec_helper'

describe Api::Plist::ProvidersController, :type => :api do
  let (:user) {Factory(:confirmed_user)}
  let (:api_account) {user.new_api_account}
  let (:api_token) {api_account.authentication_token}
  let (:email) {api_account.email}

  it "should return a successfull plist containing a list of providers" do
    get 'index', :auth_token => api_token, :email => email
    last_response.status.shoud be_success
    last_response.body.should eql({:sucess => true}.to_plist.to_s)
  end
end

Here are the posts I already read, even if they talk mainly about webrat ...:

  • http://codingfrontier.com/integration-testing-setup-with-rspec-2-and-ca
  • https://github.com/rspec/rspec-rails/issues/174
  • ...

If Anyone have an idea of what to do, which gem version to use ?


You can add...

config.include Rack::Test::Methods

to your spec_helper.rb. This will add last_response ...and other methods... to all your tests.

Since you probably don't need to do that, a better idea is to include them as needed from a module. Create a helper module in your spec/support folder. Then add this code:

module ApiHelper
  include Rack::Test::Methods
  def app
    Rails.application
  end
end

RSpec.configure do |c|
  c.include ApiHelper, type: :api
end

Now :type => :api you have will actually do something, namely add the Rack::Test::Methods to that spec.

describe Api::Plist::ProvidersController, :type => :api do
  # etc
end


last_response is a rack/test thing. IF you are not using it, it needs to be removed.


It was a misunderstanding of Rspec. "last_response" does not exist. It should be provided through a support module or whatever.

0

精彩评论

暂无评论...
验证码 换一张
取 消