首页 > 解决方案 > 在 Rails 应用程序中将“belongs_to”添加到模型后测试失败

问题描述

在 Rails 中添加 belongs_to 后测试失败

我在 Rails 应用程序中有 2 个模型:

class Micropost < ApplicationRecord
  belongs_to :user # Test failed after add this string
  validates :content, length: { maximum: 140 }, presence: true
end

class User < ApplicationRecord
  has_many :microposts

  validates :name, presence: true
  validates :email, presence: true
end

我添加了字符串“belongs_to :user”来建模“Micropost”。之后我进行了测试,但他们失败了:

rails test

  1) Failure:
MicropostsControllerTest#test_should_create_micropost [/home/kiselev/project/toy_app/test/controllers/microposts_controller_test.rb:19]:
"Micropost.count" didn't change by 1.
Expected: 3
  Actual: 2

  2) Failure:
MicropostsControllerTest#test_should_update_micropost [/home/kiselev/project/toy_app/test/controllers/microposts_controller_test.rb:38]:
Expected response to be a <3XX: redirect>, but was a <200: OK>

我有这两个测试:

test "should create micropost" do
  assert_difference('Micropost.count') do
    post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
  end

  assert_redirected_to micropost_url(Micropost.last)
end

test "should update micropost" do
  patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
  assert_redirected_to micropost_url(@micropost)
end

我有一个控制器“MicropostsController”:

class MicropostsController < ApplicationController
  before_action :set_micropost, only: [:show, :edit, :update, :destroy]

  # POST /microposts
  # POST /microposts.json
  def create
    @micropost = Micropost.new(micropost_params)

    respond_to do |format|
      if @micropost.save
        format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
        format.json { render :show, status: :created, location: @micropost }
      else
        format.html { render :new }
        format.json { render json: @micropost.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /microposts/1
  # PATCH/PUT /microposts/1.json
  def update
    respond_to do |format|
      if @micropost.update(micropost_params)
        format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
        format.json { render :show, status: :ok, location: @micropost }
      else
        format.html { render :edit }
        format.json { render json: @micropost.errors, status: :unprocessable_entity }
      end
    end
  end

设置微博:

class MicropostsControllerTest < ActionDispatch::IntegrationTest
setup do
  @micropost = microposts(:one)
end

Micropost 控制器中的参数:

def micropost_params
  params.require(:micropost).permit(:content, :user_id)
end

固定装置 Micropost:

one:
  content: MyText
  user_id: 1

two:
  content: MyText
  user_id: 1

我怎样才能改进这些测试以通过?

标签: ruby-on-railstesting

解决方案


belongs_to方法除其他外还为user. 在 rails 代码的某处,它添加了如下内容:

validates_presence_of :user

它检查用户是否存在。在您的固定装置中,您已经设置了user_id: 1. 但是在您的测试中,没有 ID 为 1 的用户。要修复它,您必须为您的 microposts 装置设置正确的用户 ID。

您可以通过以下方式进行操作。您不必定义user_id,您可以在夹具中定义关联:

one:
  content: MyText
  user: one

two:
  content: MyText
  user: one

定义一个user键而不是user_id作为值使用来自用户夹具的夹具名称 - 在测试中,users(:one)如果您想访问此夹具,它将被调用。

注意:您也可以通过添加required: false到您的belongs_to定义来删除存在验证,但我不建议这样做。


推荐阅读