简体   繁体   English

如何使用Rails中的minitest测试我的控制器中的(例如)@project.save的失败?

[英]How do I test for failure of (e.g.) @project.save in my controller using minitest in Rails?

I'm trying to improve my test coverage (just installed simplecov) and was quite pleased to have good coverage already. 我正在努力提高我的测试覆盖率(刚刚安装了simplecov)并且非常高兴能够获得良好的覆盖率。 However, I wasn't catching some failure modes in my tests, but I don't know how to force the failure of, for example, a save during creation of an object in Rails (5.2, if that matters). 但是,我没有在我的测试中捕获一些失败模式,但我不知道如何强制失败,例如,在Rails中创建对象期间的保存(5.2,如果这很重要)。 Any pointers on how to force and catch the failure of the save or update_attributes methods? 有关如何强制并捕获saveupdate_attributes方法失败的任何指针?

My main tests are working fine. 我的主要测试工作正常。 I've just got no idea how to force the code down these branches in a test! 我不知道如何在测试中强制执行这些分支的代码!

The relevant controller method: 相关控制器方法:

def create
  @project = current_user.projects.build(project_params)
  if @project.save
    flash[:notice] = "Project created!"
    redirect_to user_path(current_user)
  else
    flash[:alert] = "There was an error. Your project was not created."
    render 'new'
  end
end

The first branch of this test ( if @project.save ) is tested. 测试该测试的第一个分支( if @project.save )。 The second ( else ) isn't. 第二个( else )不是。 The test that I'm using to test success is as follows: 我用来测试成功的测试如下:

sign_in @user
assert_difference 'Project.count', 1 do
  post projects_path, params: {project: {name: @new_project_name, user_id: @user.id}}
end
assert_redirected_to user_path(@user)

What I'd really like is something where I can assert_no_difference successfully! 我真正喜欢的是我能成功assert_no_difference东西!

save returns false when there's a validation error. 当出现验证错误时, save返回false

Look at your Project model and produce parameters that are not valid. 查看您的Project模型并生成无效的参数。 For example, if there's a validates :some_field, presence: true then skip that field and validation will fail. 例如,如果有一个validates :some_field, presence: true则跳过该字段,验证将失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户未使用我的Web(导轨)应用程序(例如使用API​​),我该如何对他们进行身份验证 - How do I authenticate a user if they are not using my web (rails) application, e.g. using an API 如何获取我的Rails应用程序的基本URL(例如http:// localhost:3000)? - How do I get the base URL (e.g. http://localhost:3000) of my Rails app? 使用Rails验证,如何将一个短语列入白名单,例如“ Learn Ruby” - Using Rails validations, how do I whitelist a phrase, e.g., “Learn Ruby” 如何使用链接(例如“@test @test2”)修复渲染提及但@test2 链接到@test 的页面 - How do I fix rendering mentions with link (e.g. "@test @test2") but @test2 links to @test's page Rails-是否可能“如果@ Project.save其他@ Project.update” - Rails - is “If @Project.save Else @Project.update” possible 如何使用minitest在Rails中测试控制器的更新方法? - How to test a controller's update method in Rails using minitest? 使用 Rails Minitest 如何为我的测试数据库重新创建数据库迁移视图? - With Rails Minitest how do I recreate views on db migration for my test database? Rails,minitest:如何测试控制器的输出 - Rails, minitest: How to test the output of a controller 如何为Rails 3应用程序中的用户创建匿名电子邮件地址(例如abcd11245@craigslist.org)? - How do I create anonymous email addresses (e.g. abcd11245@craigslist.org) for users in a Rails 3 app? Rails + Rspec:如何测试环境特定的操作? 例如“......除非Rails.env.production?” - Rails + Rspec: How to test for environment specific actions? e.g. “… unless Rails.env.production?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM