gpt4 book ai didi

ruby-on-rails - RSpec redirect_to 和 return 与 redirect_to && return

转载 作者:数据小太阳 更新时间:2023-10-29 07:22:55 30 4
gpt4 key购买 nike

我有一个 Controller ,可以在特定条件下在特定点重定向。当我将参数传递给我的 Controller 规范(使用最新的 RSpec)中的规范辅助方法以触发这些条件时,我得到一个

ActionView::MissingTemplate

错误。在仔细检查我应该重定向时,我做了如下一行:

redirect_to root_path && return

然后在我的测试套件中抛出一个异常。我在应该调用的 Controller 的索引函数中放置了一个断点(我重定向到的路由指向)并且它从未在我的测试套件中调用过。当我在我的开发环境和生产环境中运行这段代码时,它似乎可以工作,但对于这个测试,它不会让步。有什么想法吗?

我的测试看起来像这样:

describe TestController do
it 'redirects properly with failure' do
get :create, provider: 'test', error: 'access_denied'
expect(response.body).to match 'test'
end
end

编辑:

更新!

似乎将我的重定向更改为

redirect_to root_path and return

在 RSpec 中工作。

我不知道为什么 && 运算符的优先级违反规范。有人对这里发生的事情有任何解释吗?

最佳答案

根据 the Rails guide :

Make sure to use and return instead of && return because && return will not work due to the operator precedence in the Ruby Language.

如果您更喜欢使用&&,请将render 的参数括在括号中:

redirect_to(root_path) && return

关于ruby-on-rails - RSpec redirect_to 和 return 与 redirect_to && return,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772996/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com