如何在视图规格中存根当前用户的属性 [英] How do I stub out a current user's attributes in a view spec
本文介绍了如何在视图规格中存根当前用户的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在查看条件输出的地方有一个视图规格.如何获得规格以返回被我嘲笑的用户?
I have a view spec where I'm testing conditional output. How do I get the spec to return the user I've mocked out?
查看文件:
.content
- if @current_user.is_welcome == true
Welcome to the site
查看规范:
before(:each) do
@user = mock_model(User)
@user.stub!(:is_welcome).and_return(true)
view.stub(:current_user).and_return(@user)
end
it "show content" do
#assign(:current_user, stub_model(User, dismiss_intro: true))
render
rendered.should have_content("Welcome to the site")
end
运行规范将返回undefined method is_welcome for nil:NilClass
推荐答案
我最终这样做了,这使我将@current_user
变量保留在自己的视图中并通过了规范:
I ended up doing this which let me keep the @current_user
variable in my view and made the spec pass:
before :each do
@user = stub_model(User, is_welcome: true)
assign(:current_user, @user)
end
然后要测试条件,只需在上下文中使用不同的before块运行另一个规范:
Then to test the conditionality, just ran another spec in a context with a different before block:
before :each do
@user = stub_model(User, is_welcome: false)
assign(:current_user, @user)
end
这篇关于如何在视图规格中存根当前用户的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文