加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Rspec Capybara:undefined last_response

发布时间:2020-12-17 03:30:03 所属栏目:百科 来源:网络整理
导读:我正在用Rspec Capybara设置我的测试环境,但是我得到了这个未定义的last_response.我在网上搜索和SO.我发现了一些关于版本的东西,使用git repo而不是 rubygem源等……但它并没有改变我的线索. 这里的回溯: ????rspec –backtrace spec / controllers / api
我正在用Rspec Capybara设置我的测试环境,但是我得到了这个未定义的last_response.我在网上搜索和SO.我发现了一些关于版本的东西,使用git repo而不是 rubygem源等……但它并没有改变我的线索.

这里的回溯:
????rspec –backtrace spec / controllers / 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配置:

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

我正在使用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 ……:

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

如果有人知道该怎么做,使用哪个宝石版本?

解决方法

你可以加…

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你实际上会做一些事情,即将Rack :: Test :: Methods添加到该规范中.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读