Hartl第11章Ajax未定义局部变量或方法`cookies’

我正在进行Hartl教程,目前正在使用AJAX进行第11.2.5章工作跟踪按钮。

对于关系控制器,我的测试失败了,我无法弄清楚原因。 预期的行为发生在浏览器中。 如果我删除cookie方法,它会导致许多其他测试失败。 我错过了什么导致了这个问题?

这是我对RelationshipController的测试

require 'spec_helper' describe RelationshipsController do let(:user) { FactoryGirl.create(:user) } let(:other_user) { FactoryGirl.create(:user) } before { sign_in user, no_capybara: true } describe "creating a relationship with Ajax" do it "should increment the Relationship count" do expect do xhr :post, :create, relationship: { followed_id: other_user.id } end.to change(Relationship, :count).by(1) end it "should respond with success" do xhr :post, :create, relationship: { followed_id: other_user.id } expect(response).to be_success end end describe "destroying a relationship with Ajax" do before { user.follow!(other_user) } let(:relationship) { user.relationships.find_by(followed_id: other_user) } it "should decrement the Relationship count" do expect do xhr :delete, :destroy, id: relationship.id end.to change(Relationship, :count).by(-1) end it "should respond with success" do xhr :delete, :destroy, id: relationship.id expect(response).to be_success end end end 

这是我的Utilities.rb

 include ApplicationHelper RSpec::Matchers.define :have_error_message do |message| match do |page| expect(page).to have_selector('div.alert.alert-error', text: message) end end def sign_in(user, options={}) if options[:no_capybara] # Sign in when not using Capybara. remember_token = User.new_remember_token cookies[:remember_token] = remember_token user.update_attribute(:remember_token, User.encrypt(remember_token)) else visit signin_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end end 

这是我的relationships_controller.rb

 class RelationshipsController < ApplicationController before_action :signed_in_user def create @user = User.find(params[:relationship][:followed_id]) current_user.follow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end def destroy @user = Relationship.find(params[:id]).followed current_user.unfollow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end end 

这是我的测试输出:

 Failures: 1) RelationshipsController creating a relationship with Ajax should increment the Relationship count Failure/Error: before { sign_in user, no_capybara: true } NameError: undefined local variable or method `cookies' for #  # ./spec/support/utilities.rb:13:in `sign_in' # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in ' 2) RelationshipsController creating a relationship with Ajax should respond with success Failure/Error: before { sign_in user, no_capybara: true } NameError: undefined local variable or method `cookies' for #  # ./spec/support/utilities.rb:13:in `sign_in' # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in ' 3) RelationshipsController destroying a relationship with Ajax should decrement the Relationship count Failure/Error: before { sign_in user, no_capybara: true } NameError: undefined local variable or method `cookies' for # # ./spec/support/utilities.rb:13:in `sign_in' # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in ' 4) RelationshipsController destroying a relationship with Ajax should respond with success Failure/Error: before { sign_in user, no_capybara: true } NameError: undefined local variable or method `cookies' for # # ./spec/support/utilities.rb:13:in `sign_in' # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in ' Finished in 0.06635 seconds 4 examples, 4 failures Failed examples: rspec ./spec/models/relationship_spec.rb:12 # RelationshipsController creating a relationship with Ajax should increment the Relationship count rspec ./spec/models/relationship_spec.rb:18 # RelationshipsController creating a relationship with Ajax should respond with success rspec ./spec/models/relationship_spec.rb:29 # RelationshipsController destroying a relationship with Ajax should decrement the Relationship count rspec ./spec/models/relationship_spec.rb:35 # RelationshipsController destroying a relationship with Ajax should respond with success 

我注意到你把RelationshipsController的测试放在spec/models 。 这真的是教程所说的吗? 尝试将其放在spec/controllers以便将其视为控制器规范。

我一直在努力解决这个问题,以及很多其他人的看法。

我的解决方案(我怀疑它不是正确的)是改变线:

 before { sign_in user, no_capybara: false } 

 before { sign_in user } 

不知道为什么,但看起来我的rspec测试看不到utilities.rb中的代码