rspec 视图存根和部分 [英] rspec view stubs and partials
问题描述
我正在使用 RSpec(Rails 3.2.8 上的 2.12)测试视图.我正在使用 CanCan 有条件地在页面上显示某些元素.这需要一个控制器方法current_user".在我的一些规范中,我已经能够排除 current_user,例如.controller.stub(:current_user).and_return(etc)或者视图.存根.等.
I'm testing a view with RSpec (2.12 on Rails 3.2.8). I'm using CanCan to conditionally display certain elements on a page. This requires a controller method 'current_user'. In some of my specs I've been able to stub out current_user, eg. controller.stub(:current_user).and_return(etc) or view.stub.etc .
这适用于我的一些规格.但我有几个地方无法正常工作,我不明白为什么.
This works for some of my specs. But I've got a couple where it's not working and I don't understand why.
它不工作的两个规范测试了一个视图,它调用了一个局部,在局部内部我访问了current_user"作为一个方法.错误是
The two specs where it's not working test a view, which calls down into a partial, and inside the partial I access 'current_user' as a method. The error is
undefined local variable or method `current_user'
所以我想我的问题是如何正确地存根方法,以便可以在部分内部访问它们.
So I guess my question is how to stub methods correctly so that they can be accessed down inside partials.
应该怎么做?
推荐答案
控制器存根不起作用,因为您不是在测试控制器,而是在测试视图.只需使用视图存根:
A controller stub won't work because you're not testing a controller, you're testing a view. Just use a view stub instead:
view.stub(:current_user).and_return(etc)
这应该在局部和视图中都有效.
This should work in a partial as well as in a view.
这篇关于rspec 视图存根和部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!