RSPEC未定义局部变量或方法"响应"

Bee*_*gie 3 ruby rspec ruby-on-rails

我对'响应'的错误是...

规格/功能/ test_name_spec.rb

require "spec_helper"

describe "the signin process", :type => :feature do
  it "does not sign me in if I use the wrong password" do
    visit '/users/sign_in'
    within("#new_user") do
      user = Fabricate(:user, email: 'user@example.com', password: 'password')
      fill_in 'Email', :with => 'user@ example.com'
      fill_in 'Password', :with => 'badone'
    end
    click_button 'Log in'
    expect(response).to render_template(:new)
  end
end
Run Code Online (Sandbox Code Playgroud)

宝石文件

group :test do
  gem 'shoulda-matchers', '~> 2.8', require: false # now loaded inside of spec/spec_helper.rb
  gem 'capybara', '~>2.4'
  gem 'launchy'
  gem 'capybara-email', github: "dockyard/capybara-email"
  gem 'database_cleaner', '~> 1.5', '>= 1.5.1'
end
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为上面的测试成功使用expect但是 page

expect(page).to have_content 'Signed in successfully.'
Run Code Online (Sandbox Code Playgroud)

这可能会发生什么?

编辑

我把它改成了......

expect(page).to have_content 'Log in'
Run Code Online (Sandbox Code Playgroud)

这基本上是一回事,但我仍然想知道为什么response错了.它是不是在Capybara中可用,还是有另一个问题为什么那不起作用?

Fre*_*ung 9

你正在编写一个功能规范.使用功能规范,没有响应对象.根据设计,使用功能规范,您不会使用低级别的问题进行测试,例如呈现的模板,但是更高级别的问题,例如页面上的预期内容.

您可能对请求规范感兴趣,这些规范映射到rails的集成测试,从而允许跨越多个请求但确实暴露响应方法的规范.

但是你不能(或至少不应该)使用水豚(visit,click_button,等),要求规范内部-多小时已经失去了该归结为混合集成测试语言(的问题get,post,response)和水豚.这两者存在于完全独立的世界中,例如由水豚触发的请求对其没有影响,response并且触发的请求对其get没有影响page.