如何在请求规范中存根 ApplicationController 方法
我需要存根 Rspec/capybara 请求规范中 current_user
方法的响应。该方法在 ApplicationController
中定义并使用 helper_method。该方法应该只返回一个用户 ID。在测试中,我希望此方法每次都返回相同的用户 ID。
或者,我可以通过在规范中设置 session[:user_id]
(这是 current_user
返回的内容)来解决我的问题...但这似乎也不起作用。
其中任何一个都可能吗?
编辑:
这是我所得到的(它不起作用。它只是运行正常的 current_user 方法)。
require 'spec_helper'
describe "Login" do
before(:each) do
ApplicationController.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
也不起作用:
require 'spec_helper'
describe "Login" do
before(:each) do
@mock_controller = mock("ApplicationController")
@mock_controller.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
I am needing to stub the response of a current_user
method in an Rspec/capybara request spec. The method is defined in ApplicationController
and is using helper_method. The method should simply return a user id. Within the test, I'd like this method to return the same user id each time.
Alternatively, I could fix my problem by setting session[:user_id]
in the spec (which is what current_user
returns)... but that doesn't seem to work either.
Are either of these possible?
Edit:
Here is what I've got (it is not working. It just runs the normal current_user method).
require 'spec_helper'
describe "Login" do
before(:each) do
ApplicationController.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
Also not working:
require 'spec_helper'
describe "Login" do
before(:each) do
@mock_controller = mock("ApplicationController")
@mock_controller.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
skalee 似乎在评论中提供了正确的答案。
如果您尝试存根的方法是实例方法(最有可能)而不是类方法,那么您需要使用:
ApplicationController.any_instance.stub(:current_user)
skalee seems to have provided the correct answer in the comment.
If the method you're trying to stub is an instance method (most likely) and not a class method then you need use:
ApplicationController.any_instance.stub(:current_user)
以下是基本形式的几个示例。
在您的特定情况下,我认为正确的形式是:
这是我所从事的项目的完整工作示例:
为了解释我的完整示例,基本上它的作用是验证,当
ActionController::InvalidAuthenticityToken 错误,出现一条 Flash 消息,并且用户将被重定向到
方法可用给你。portals_controller#index
操作。您可以使用这些表单来存根并返回特定值、测试引发的给定错误的实例等。有多种 .stub(:action_name).and_[do_something_interesting]()更新(添加代码后):根据我的评论,更改您的代码,使其内容如下:
Here are a couple of examples of the basic form.
In your particular case, I believe the proper form would be:
Here's a full working example from a project I work on:
To explain my full example, basically what this does is verify that, when an
ActionController::InvalidAuthenticityToken
error is raised anywhere in the app, that a flash message appears, and the user is redirected to theportals_controller#index
action. You can use these forms to stub out and return specific values, test an instance of a given error being raised, etc. There are several.stub(:action_name).and_[do_something_interesting]()
methods available to you.Update (after you added your code): per my comment, change your code so it reads:
这对我有用,并为我提供了一个在测试中使用的
@current_user
变量。我有一个如下所示的助手:
然后在我的请求规范中,我调用:
This works for me and gives me a
@current_user
variable to use in tests.I have a helper that looks like this:
And then in my request specs, I call:
对于 Rspec 3+,新的 api 是:
对于控制器测试,简洁明了:
或者对于 ApplicationController 的所有实例:
For Rspec 3+ the new api is:
For a controller test, nice and short:
Or for all instances of ApplicationController:
对于任何其他碰巧需要存根设置 ivar 的应用程序控制器方法的人(并且因无休止地自慰为什么不应该这样做而受阻),这里有一种可行的方法,具有大约 2013 年 10 月的 Rspec 风格。
它存根该方法不执行任何操作,并在 rspec 的控制器实例上设置 ivar,并将其作为 @campaign 提供给测试。
For anyone else who happens to need to stub an application controller method that sets an ivar (and was stymied by endless wanking about why you shouldn't do that) here's a way that works, with the flavour of Rspec circa October 2013.
it stubs the method to do nothing, and sets the ivar on rspec's controller instance, and makes it available to the test as @campaign.
提供的回复都不适合我。正如 @matt-fordam 的原始帖子中一样,我有一个请求规范,而不是控制器规范。该测试仅渲染视图而不启动控制器。
我通过在视图上存根方法解决了这个问题,如其他SO帖子中所述
None of the provided responses worked for me. As in @matt-fordam's original post, I have a request spec, not a controller spec. The test just renders the view without launching a controller.
I resolved this by stubbing the method on the view as described in this other SO post