我有如下所示的控制器方法:
class TestController < ApplicationController
def testAction
render :json => { 'success'=>1 }.to_json
end
end
当我在浏览器中加载这个动作时,我得到了我所期望的:{"success":1}
然而,当使用 RSpec 进行测试时,response.body
给了我'<html><body>You are being <a href="https://test.host/test/testAction">redirected</a>.</body></html>'
我注意到我所有的控制器都发生了这种奇怪的重定向。如何阻止这种重定向的发生,以便对响应正文进行检查?
谢谢!
- - - - - - - - - - - - 编辑:
知道为什么会发生以下测试失败吗?
# app/controllers/test_controller.rb
def test
flash[:notice] = 'test'
end
# spec/controllers/test_controller_spec.rb
describe TestController, "calling the test() method" do
it "should set the flash notice" do
put :test
flash[:notice].should_not be_blank
end
end
# result
'TestController calling the test() method should set the flash notice' FAILED
expected blank? to return false, got true