2011-08-04 105 views
5

我用Rspec + Capybara設置我的測試環境,但是我得到了這個未定義的last_response。我正在通過網絡搜索,所以。我發現了幾個關於版本的東西,使用git repo而不是rubygem源碼等等......但它並沒有改變我的線索。Rspec +水豚:undefined last_response

這裏回溯: rspec的--backtrace規格/控制器/ API/plist中/ providers_listing_spec.rb 失敗:

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/rspec-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'` 

這裏是我的測試寶石配置:

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 

我'm使用database_clearner來處理mongo db ...

直到現在我還沒有真正使用:

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 

這裏是我已經看過的職位,即使他們主要談webrat ...:

如果任何人都知道該做什麼,使用哪個寶石版本?

回答

5

您可以添加...

config.include Rack::Test::Methods 

您spec_helper.rb。這將添加last_response ...和其他方法...到全部您的測試。

由於您可能不需要這樣做,更好的辦法是根據需要將它們包含在模塊中。在spec/support文件夾中創建一個輔助模塊。然後添加以下代碼:

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

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

現在:type => :api你居然會做的事,即測試::方法添加機架::該規範。

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

這是對Rspec的誤解。 「last_response」不存在。應該通過支持模塊或其他方式提供。

0

last_response是一個機架/測試的東西。如果你沒有使用它,它需要被刪除。