gpt4 book ai didi

ruby-on-rails - 将 'belongs_to' 添加到 Rails 应用程序中的模型后测试失败

转载 作者:行者123 更新时间:2023-11-28 21:16:15 26 4
gpt4 key购买 nike

在 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

我有一个 Controller “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

微博 Controller 中的参数:

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

Fixtures 微博:

one:
content: MyText
user_id: 1

two:
content: MyText
user_id: 1

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

最佳答案

belongs_to方法还为 user 添加了存在验证。在 Rails 代码的某处,它添加了如下内容:

validates_presence_of :user

然后检查用户是否存在。在您的设备中,您设置了 user_id: 1。但是在您的测试中,没有以 1 作为 ID 的用户。要修复它,您必须为您的微博固定装置设置正确的用户 ID。

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

one:
content: MyText
user: one

two:
content: MyText
user: one

定义一个 user 键而不是 user_id 并且使用来自用户灯具的灯具名称作为值 - 在测试中它将被称为 users( :one) 如果你想访问这个装置。

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

关于ruby-on-rails - 将 'belongs_to' 添加到 Rails 应用程序中的模型后测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com