与工厂女孩一起进行 Rspec 控制器测试
我怀疑我对此非常愚蠢并且错过了一些明显的事情。但我对 rspec 和工厂女孩相当陌生,无法让这个简单的测试发挥作用。
基本上我已经建立了一个名为 page 的模型,并在控制器中调用它,如下所示:
@pages = Page.where(:page_type => "homepage").limit(2)
在我的测试中,我执行以下操作:
before do
@pages = [Factory(:page), Factory(:page, :title=> "Contact", :content=>"contact content", :meta=>"meta content", :page_type=>'homepage') ]
get 'index'
end
it 'should set the pages variable' do
assigns[:pages].should_not be_nil
assigns[:pages].length == 2;
end
但我得到的只是:
Failure/Error: assigns[:pages].should_not be_nil
expected not nil, got nil
非常感谢任何帮助
I suspect I just being very dumb with this and missing something obvious. But I fairly new to rspec and factory girl and cannot get this simple test to work.
Basically I have set up a model called page and am calling it in the controller like this:
@pages = Page.where(:page_type => "homepage").limit(2)
for in my test I do the following:
before do
@pages = [Factory(:page), Factory(:page, :title=> "Contact", :content=>"contact content", :meta=>"meta content", :page_type=>'homepage') ]
get 'index'
end
it 'should set the pages variable' do
assigns[:pages].should_not be_nil
assigns[:pages].length == 2;
end
yet all I get is:
Failure/Error: assigns[:pages].should_not be_nil
expected not nil, got nil
ANy help greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的控制器正在将其
@pages
实例变量设置为 nil。最有可能的是它根本没有被分配到。您确定您粘贴的代码片段:正在控制器的
index
操作中调用吗?如果是这样,该代码会将@pages
设置为空列表,而不是 nil。Your controller is setting its
@pages
instance variable to nil. Most likely it's not being assigned to at all. Are you sure the code snippet which you pasted:is being called in the
index
action of your controller? If so, that code would set@pages
to the empty list, not nil.