gpt4 book ai didi

ruby-on-rails - 在 Minitest 中测试更新操作

转载 作者:行者123 更新时间:2023-12-02 01:24:17 30 4
gpt4 key购买 nike

我对创建进行了测试:

  test "should create article" do
assert_difference('Article.count') do
post :create, article: {title: "Test", body: "Test article."}
end
assert_redirected_to article_path(assigns(:article))
end

我想对 update 操作执行类似的操作。

我的更新操作如下:

  def update 
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end

我正在考虑这样的事情:

  test "should update article" do
patch :update, article {title: "Updated", body: "Updated article."}
end

但我的问题是:如何检查我的文章是否在 Minitest 中更新?以及如何找到我要更新的项目?在装置中我有两篇文章。

最佳答案

您应该能够将您的固定文章之一分配给变量,并在更新后对文章运行断言,如下所示(我尚未测试此代码,只是为了说明测试结构​​):

test "should update article" do
article = articles(:article_fixture_name)
updated_title = "Updated"
updated_body = "Updated article."

patch :update, article: { id: article.id, title: updated_title, body: updated_body }

assert_equal updated_title, article.title
assert_equal updated_body, article.body
end

您可能想要初始化article作为 setup 中的实例变量方法并将其设置为nil在你的teardown方法,或者您管理设置/拆卸以确保您的起始状态在测试之间保持一致。

关于ruby-on-rails - 在 Minitest 中测试更新操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32310851/

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