0

I'm not sure what I did wrong as I am completely new to RoR. The errors focus around local variables or methods not being defined. I'm quite lost as to how most of the methods and such from files relate to other and can be used in others and so I'm not really sure how where to look to fix the undefined methods/ local variables. If any other files are needed then I can provide it upon request.

Failures:

  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: "[email protected]",
                     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
2
  • Your relationship.rb really contains that spec code or you wrongly posted it?
    – Pavan
    Jul 31, 2014 at 14:06
  • Wrongly posted, I fixed it.
    – Verendus
    Jul 31, 2014 at 15:05

1 Answer 1

1

In relationship_spec.rb, you have a misplaced end which is causing the example group for Relationship getting closed beforehand so the rest of the example groups which should have been nested under it have no access of the local variables follower, followed and relationship and obviously no access to the subject that you intended to use in your examples i.e., relationship. This is causing the errors.

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"

NOTE: Proper indentation can really help you to detect this kind of issues in future.

1
  • I had a feeling it had to do with the ends, because I have had syntax problems with it before, but this time I wasn't sure which it was and if it was even the source of the problem. Thanks a bunch!
    – Verendus
    Jul 31, 2014 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.