第11章Michael Hartl RoR Rspec在11.1.5结束时失败(6个错误)

时间:2014-07-31 13:59:28

标签: ruby-on-rails rspec

我不确定我做错了什么,因为我对RoR完全不熟悉。错误集中在局部变量或未定义的方法。我很遗憾大多数方法等文件与其他方法有关,并且可以在其他方法中使用,所以我不确定如何修复未定义的方法/局部变量。如果需要任何其他文件,我可以根据要求提供。

故障:

  1) when follower id is not present 
     Failure/Error: before { relationship.follower_id = nil }
     NameError:
       undefined local variable or method `relationship' for #<RSpec::Core::ExampleGroup::Nested_5:0x000000064cf4b8>
     # ./spec/models/relationship_spec.rb:27:in `block (2 levels) in <top (required)>'

  2) when followed id is not present 
     Failure/Error: before { relationship.followed_id = nil }
     NameError:
       undefined local variable or method `relationship' for #<RSpec::Core::ExampleGroup::Nested_4:0x000000062511c8>
     # ./spec/models/relationship_spec.rb:22:in `block (2 levels) in <top (required)>'

  3) follower methods 
     Failure/Error: it { should respond_to(:followed) }
       expected "follower methods" to respond to :followed
     # ./spec/models/relationship_spec.rb:16:in `block (2 levels) in <top (required)>'

  4) follower methods 
     Failure/Error: it { should respond_to(:follower) }
       expected "follower methods" to respond to :follower
     # ./spec/models/relationship_spec.rb:15:in `block (2 levels) in <top (required)>'

  5) follower methods followed 
     Failure/Error: its(:followed) { should eq followed }
     NameError:
       undefined local variable or method `followed' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_2:0x00000006276158>
     # ./spec/models/relationship_spec.rb:18:in `block (2 levels) in <top (required)>'

  6) follower methods follower 
     Failure/Error: its(:follower) { should eq follower }
     NameError:
       undefined local variable or method `follower' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x00000006280ea0>
     # ./spec/models/relationship_spec.rb:17:in `block (2 levels) in <top (required)>'

relationship_spec.rb

require 'spec_helper'

describe Relationship do

  let(:follower) { FactoryGirl.create(:user) }
  let(:followed) { FactoryGirl.create(:user) }
  let(:relationship) { follower.relationships.build(followed_id: followed.id) }

  subject { relationship }

  it { should be_valid }
end

  describe "follower methods" do
    it { should respond_to(:follower) }
    it { should respond_to(:followed) }
    its(:follower) { should eq follower }
    its(:followed) { should eq followed }
  end

  describe "when followed id is not present" do
    before { relationship.followed_id = nil }
    it { should_not be_valid }
  end

  describe "when follower id is not present" do
    before { relationship.follower_id = nil }
    it { should_not be_valid }
  end

user.spec.rb

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com",
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:remember_token) }
  it { should respond_to(:authenticate) }
  it { should respond_to(:admin) }
  it { should respond_to(:microposts) }
  it { should respond_to(:feed) }
  it { should respond_to(:relationships) }
  it { should respond_to(:followed_users) }
  it { should respond_to(:reverse_relationships) }
  it { should respond_to(:followers) }
  it { should respond_to(:following?) }
  it { should respond_to(:follow!) }
  it { should respond_to(:unfollow!) }

  it { should be_valid }
  it { should_not be_admin }
.
.
.
  describe "following" do
    let(:other_user) { FactoryGirl.create(:user) }
    before do
      @user.save
      @user.follow!(other_user)
    end

    it { should be_following(other_user) }
    its(:followed_users) { should include(other_user) }

    describe "followed user" do
      subject { other_user }
      its(:followers) { should include(@user) }
    end

    describe "and unfollowing" do
      before { @user.unfollow!(other_user) }

      it { should_not be_following(other_user) }
      its(:followed_users) { should_not include(other_user) }
    end
  end
end

user.rb

class User < ActiveRecord::Base
   has_secure_password
  has_many :microposts, dependent: :destroy
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :reverse_relationships, foreign_key: "followed_id",
                                   class_name:  "Relationship",
                                   dependent:   :destroy
  has_many :followers, through: :reverse_relationships, source: :follower
  has_many :followed_users, through: :relationships, source: :followed
  before_save { email.downcase! }
  before_create :create_remember_token
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

  validates :password, length: { minimum: 6 }

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.digest(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  def feed
    # This is preliminary. See "Following users" for the full implementation.
    Micropost.where("user_id = ?", id)
  end

  def following?(other_user)
    relationships.find_by(followed_id: other_user.id)
  end

  def follow!(other_user)
    relationships.create!(followed_id: other_user.id)
  end

  def unfollow!(other_user)
    relationships.find_by(followed_id: other_user.id).destroy
  end

  private

    def create_remember_token
      self.remember_token = User.digest(User.new_remember_token)
    end
end

relationship.rb

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

1 个答案:

答案 0 :(得分:1)

relationship_spec.rb中,您错位end导致Relationship的示例组事先被关闭,因此本应嵌套在其下的其他示例组没有访问局部变量followerfollowedrelationship,显然无法访问您打算在示例中使用的subjectrelationship。这导致错误。

require 'spec_helper'

describe Relationship do

  let(:follower) { FactoryGirl.create(:user) }
  let(:followed) { FactoryGirl.create(:user) }
  let(:relationship) { follower.relationships.build(followed_id: followed.id) }

  subject { relationship }

  it { should be_valid }
## end  ## <<-- REMOVED this end of "describe Relationship"

  describe "follower methods" do
    it { should respond_to(:follower) }
    it { should respond_to(:followed) }
    its(:follower) { should eq follower }
    its(:followed) { should eq followed }
  end

  describe "when followed id is not present" do
    before { relationship.followed_id = nil }
    it { should_not be_valid }
  end

  describe "when follower id is not present" do
    before { relationship.follower_id = nil }
    it { should_not be_valid }
  end 
end  ## <-- ADDED end of "describe Relationship"

注意:正确的缩进可以帮助您在将来检测此类问题。