我有以下内容:
test "should update deal" do
patch user_deal_url(@user, @deal), params: { deal: { name: @deal.name+'xxx' } }
# p @response.body
# p @deal.errors.full_messages
assert_select ".alert", false # should not have alert
assert_redirected_to dashboard_url
follow_redirect!
assert_select ".alert", "Deal was successfully updated."
end
它只说:
Failure:
DealsControllerTest#test_should_update_deal [C:/Users/Chloe/workspace/test/controllers/deals_controller_test.rb:73]:
Expected response to be a <3XX: redirect>, but was a <200: OK>
我以为我可以打印@deal.errors.full_messages
,但它与控制器中的@deal
不同。我能够打印整个响应正文并将其复制到Notepad ++中并搜索错误消息,但它太乏味了。我以为我可以断言.alert
不存在,它会告诉我它到底是什么。我添加了
assert_select "#error_explanation", false # should not have error
但那只告诉我它存在,而不是它失败的原因。
Expected exactly 0 elements matching "#error_explanation", found 1..
Expected: 0
Actual: 1
那么如何快速确定模型在测试期间无法在控制器中保存或更新的原因?
Rails 5.0.2
参考:http://guides.rubyonrails.org/testing.html
我的解决方案导致后续的assert_select
语句失败!只让puts css_select('#error_explanation')
使最后一个assert_select
失败,但将其评论出来会使其成功。为什么呢?
我同时输入了一个Rails错误:https://github.com/rails/rails/issues/29367
答案 0 :(得分:0)
我能够放
puts css_select('#error_explanation')
在put
/ patch
之后,它会打印
# Running:
<div id="error_explanation">
<h2>
2 errors
prohibited this deal from being saved:
</h2>
<ul>
<li>Size can't be blank</li>
<li>Size is not a number</li>
</ul>
</div>
F
Failure:
我仍然在寻找更好的东西,因为留下这一行导致后续assert_select
失败!