【问题标题】:Hartl Chapter 11 Ajax undefined local variable or method `cookies'Hartl 第 11 章 Ajax 未定义的局部变量或方法 `cookies'
【发布时间】:2013-12-10 23:17:46
【问题描述】:

我正在学习 Hartl 教程,目前正在第 11.2.5 章学习 AJAX 的工作关注按钮。

我的关系控制器测试失败,我不知道为什么。预期的行为发生在浏览器中。如果我删除 cookies 方法,它会导致许多其他测试失败。我错过了什么导致这个问题?

这是我对关系控制器的测试

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

这是我的关系控制器.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 #     <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc7f827f930>
      # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'

  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 #    <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc7f8516a90>
     # ./spec/support/utilities.rb:13:in `sign_in' 
    # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'

 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 #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fc7f85367f0>
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'

  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 #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fc7f8556938>
    # ./spec/support/utilities.rb:13:in `sign_in'
    # ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'

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

【问题讨论】:

  • 请解释“我的测试失败”是什么意思。有错误信息吗?你能发布完整的测试输出吗?
  • 添加了测试输出。 :)

标签: ruby-on-rails ajax rspec


【解决方案1】:

我注意到您在spec/models 中对RelationshipsController 进行了测试。这真的是教程所说的吗?尝试将其放在spec/controllers 下,以便将其视为控制器规范。

【讨论】:

  • 就是这样~!我读错了教程谢谢(我的大脑在第 11 章已经液化)。所有测试都是绿色的。谢谢大卫。
【解决方案2】:

从外观上看,我和很多其他人一样一直在努力解决这个问题。

我的解决方案(我怀疑这不是正确的)是更改线路:

before { sign_in user, no_capybara: false }

before { sign_in user }

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

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2014-09-24
    相关资源
    最近更新 更多