我的 Controller 中有以下代码:
format.json { render :json => {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}
在我的 RSpec Controller 测试中,我想验证某个场景确实收到了成功的 json 响应,所以我有以下行:
controller.should_receive(:render).with(hash_including(:success => true))
虽然我在运行测试时收到以下错误:
Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
(#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
expected: 1 time
received: 0 times
我检查的响应有误吗?
最佳答案
您可以像这样解析响应正文:
parsed_body = JSON.parse(response.body)
然后,您可以针对已解析的内容做出断言。
parsed_body["foo"].should == "bar"
https://stackoverflow.com/questions/5159882/