Rails:使用 RSpec 测试控制器时,如何阻止它们重定向?

发布于 2024-08-19 08:33:03 字数 944 浏览 5 评论 0原文

我的控制器方法如下所示:

class TestController < ApplicationController
  def testAction
    render :json => { 'success'=>1 }.to_json
  end
end

当我在浏览器中加载此操作时,我得到了我期望的结果: {"success":1}

但是,当使用 RSpec 进行测试时,response. body 给我 '您正在被 重定向。< /body>'

我注意到我的所有控制器都发生了这种奇怪的重定向。如何阻止这种重定向发生,以便我可以对响应正文进行检查?

谢谢!

----------------------- 编辑:

知道为什么会发生以下测试失败吗?

# 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

I have controller methods that look like this:

class TestController < ApplicationController
  def testAction
    render :json => { 'success'=>1 }.to_json
  end
end

When I load this action in the browser, I get what I expect: {"success":1}

When testing with RSpec, however, response.body gives me '<html><body>You are being <a href="https://test.host/test/testAction">redirected</a>.</body></html>'

I've noticed that this weird redirection is happening with all of my controllers. How can I stop this redirection from happening, so I can run checks on the response body?

Thanks!

----------------------- EDIT:

Any idea why the following test failure is happening?

# 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若相惜即相离 2024-08-26 08:33:03

将您的断言更改为

response.should be_redirect

至少,如果它正在做正确的事情:)

Change your assertion to be

response.should be_redirect

At least, if it's doing the right thing :)

陌伤浅笑 2024-08-26 08:33:03

我很困惑为什么你的测试输出显示“ApiController 调用 test() 方法...”,而你的示例似乎是针对 TestController 的。我自己运行了几个例子,似乎得到了正确的结果。我建议尝试使用 Ruby 调试器 (gem ruby​​-debug) 来诊断此问题。这样您就可以确认正在调用正确的代码以及交互式检查值。

I'm confused why your test output says 'ApiController calling the test() method...' when your example seems to be for TestController. I've run a couple of examples of my own and I seem to get the correct results. I suggest trying the Ruby debugger (gem ruby-debug) to diagnose this. That way you can confirm that the proper code is being called as well as interactively check values.

鸵鸟症 2024-08-26 08:33:03

根据定义,您正在控制器上执行单元测试,因此不应尝试查看响应中的内容,而应仅查看发生了正确的操作并完成了应该执行的所有操作。测试响应的内容是像 Cucumber 这样的集成测试的工作。

本质上,尝试断言控制器尝试重定向。

@response.should be_redirect

By definition, you are performing a unit test on the controller and therefore should not be trying to see what's in the response, only that the correct action happened and did everything it was supposed to do. Testing the contents of the response is a job for an integration test like Cucumber.

Essentially, try asserting that the controller attempted a redirect.

@response.should be_redirect
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文