关系控制器的Rspec失败,无法找到具有' id' =的用户

时间:2016-05-03 10:47:45

标签: ruby-on-rails unit-testing rspec rspec-rails

我正在尝试为我的人际关系控制器制作一个规范,并且#create方法失败了(无法找到用户' id' =)。 你能告诉我如何解决这个问题吗? 感谢您的帮助,如果您有任何疑问,请询问。 :)

relationships_controller:

class RelationshipsController < InheritedResources::Base

  def create
    user = User.find(params[:followed_id])
    current_user.follow(user)
    redirect_to user
  end

  def destroy
    user = Relationship.find(params[:id]).followed
    current_user.unfollow(user)
    redirect_to user
  end
end

relationships_controller_spec:

require 'rails_helper'

describe RelationshipsController do
  let(:relationship) { create(:relationship) }
  let(:user) { create(:user) }

  before do
    sign_in :user, create(:user)
  end


  describe '#create' do
    let!(:user) { create(:user) }
    it "should require logged-in user to create relationship" do
      post :create, followed_id: user.id
      expect{
        Relationship.create
      }.to change(Relationship, :count).by(1)
      redirect_to root_path
    end
  end


  describe '#create' do
    let!(:relationship) { create(:relationship) }

    it "should require logged-in user to destroy relationship" do
      expect {
        delete :destroy, id: relationship.id
      }.to change(Relationship, :count).by(-1)
      redirect_to root_path
    end
  end
end

失败:

RelationshipsController
  #create
    should require logged-in user to destroy relationship
  #create
    should require logged-in user to create relationship (FAILED - 1)

Failures:

  1) RelationshipsController#create should require logged-in user to create relationship
     Failure/Error:
       expect{
         Relationship.create
       }.to change(Relationship, :count).by(1)

       expected #count to have changed by 1, but was changed by 0

关系:

class Relationship < ActiveRecord::Base
  #Associations
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

#Validations
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

用户:

class User < ActiveRecord::Base
      # Associations
      has_many :active_relationships, class_name:  "Relationship",
                                      foreign_key: "follower_id",
                                      dependent:   :destroy
      has_many :passive_relationships, class_name:  "Relationship",
                                      foreign_key: "followed_id",
                                      dependent:   :destroy
      has_many :following, through: :active_relationships, source: :followed
      has_many :followers, through: :passive_relationships, source: :follower

      # Follows a user.
      def follow(other_user)
        active_relationships.create(followed_id: other_user.id)
      end
    end

3 个答案:

答案 0 :(得分:2)

在规范中传递:followed_id并再次运行

describe '#create' do
it "should require logged-in user to create relationship" do
  post :create, followed_id: user.id
  expect{
    Relationship.create
  }.to change(Relationship, :count).by(1)
  redirect_to root_path
end

答案 1 :(得分:1)

在你的考试中 -

describe RelationshipsController do
  let(:relationship) { create(:relationship) }
  let(:user) { create(:user) }

直到需要才创建用户。

describe RelationshipsController do
  let(:relationship) { create(:relationship) }
  let!(:user) { create(:user) }

使用! operator强制创建用户变量并将其存储到数据库中。否则变量是延迟加载的(仅在需要时)。这就是数据库中无法使用的原因

答案 2 :(得分:0)

这解决了:

  describe '#create' do
    let!(:followed) { create(:user) }
    it "should require logged-in user to create relationship" do
      expect{
        post :create, followed_id: followed.id
      }.to change(Relationship, :count).by(1)
      redirect_to root_path
    end
  end