【问题标题】:Rspec failure for relationships controller, Couldn't find User with 'id'=关系控制器的 Rspec 失败,找不到具有 'id'= 的用户
【发布时间】:2016-05-03 10:47:45
【问题描述】:

我正在尝试为我的关系控制器制定规范,但 #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

【问题讨论】:

  • 你还没有发布followed_id参数
  • 我的意思是在您的控制器规范中,当您发出发布请求时
  • 另外,您的期望是手动创建关系然后检查计数。这实际上并不是在测试您的控制器。
  • @AaronWashburn 你能否解释一下你的评论,也许有一个例子或教程。谢谢
  • @Jony,您正在调用 Relationship.create 然后检查是否创建了关系。这是在测试您的模型,而不是您的控制器。如果您查看销毁规范,您将直接使用销毁控制器操作来更改关系计数。你想为你的创建动作做同样的事情。然后,不要忘记正确命名您的测试描述,以便其他人知道您正在尝试做什么。

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


【解决方案1】:

在规范中传递 :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

结束

【讨论】:

    【解决方案2】:

    在你的测试中 -

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

    直到需要时才会创建用户。

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

    使用 !运算符强制创建用户变量并将其存储到数据库中。否则,该变量是延迟加载的(仅在需要时)。这就是为什么它在数据库中不可用

    【讨论】:

      【解决方案3】:

      这解决了:

        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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多