如何在 Capybara 验收测试中存根控制器/current_user 方法
我有一个调用 current_user.eligible_for_reward 的辅助方法?我想在水豚验收规范中存根该调用。在我的模型中,我有一个占位符虚拟方法:
User.rb
def eligible_for_reward?
"blah"
end
这是我迄今为止尝试过的方法:
我尝试存根 current_user,但这会导致“修改冻结对象”错误,因为控制器为零。我相信只有当我使用 RSpec 的 get 而不是访问 some_path 时才会设置控制器。我相信,我需要坚持访问 some_path,否则我的页面。应该...全部失败,因为页面未设置。
我尝试以用户身份登录(login_as(user),成功通过注册/登录运行),然后存根 user.eligible_for_reward?,但这似乎不起作用,因为我仍在返回测试“ blah"
有没有一个好的方法来消除这个方法?
I have a helper method that calls current_user.eligible_for_reward? and I want to stub that call in a Capybara acceptance spec. In my model I have a placeholder dummy method:
User.rb
def eligible_for_reward?
"blah"
end
Here is what I've tried so far:
I tried stubbing current_user, but that results in a "modify frozen object" error because controller is nil. I believe controller will only be set if I use RSpec's get instead of visit some_path. I need to stick with visit some_path, I believe, otherwise my page.should ... all fail because page is not set.
I tried logging in as user (login_as(user) which successfully runs through the register/login) and then stubbing user.eligible_for_reward?, but that doesn't seem to work, as I'm still getting back the test "blah"
Is there a good way to stub out this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在验收规范中进行存根并不是一个好主意。如果没有其他方法,那么你可以这样做:
只需记住,在验收规范中存根/模拟事物意味着你实际上没有测试任何东西。
Stubbing in acceptance specs is not a good idea. If there is no other way then you can just do:
Just remember that stubbing/mocking things in acceptance specs means that you really don't test anything.