在Rspec视图中使用Factory Girl [英] Using Factory Girl with Rspec Views
问题描述
所以我想使用Rspec和Factory Girl测试一些视图,但是我不知道如何正确地为视图分配工厂.我在网上看到的所有信息都使用存根,尽管存根很好,但我想使我的rspec内容保持一定的一致性.
So I want to test some views using Rspec and Factory Girl, but I don't know how to assign a factory to a view properly. All of the information I see online uses stubbing, and although stubbing is fine, I want to keep my rspec stuff somewhat consistent.
我想为版本模型使用工厂,并在渲染页面时将其与页面相关联,但是我认为的所有内容似乎都已损坏.这是一个荒谬的例子:
I want to use a factory for an edition model and associate that with the page when I render the page, but everything I've considered seems to be broken. Here's sort of a ridiculous example:
require 'spec_helper'
describe "catalog/editions/rationale.html.haml" do
before do
@edition = Factory.create(:edition)
assign(:editions, @edition)
render :action => 'rationale', :id => @edition
end
context "GET rationale" do
it "should not have the preamble to the United States constitution" do
rendered.should_not contain(/We the People of the United States/)
end
end
end
在这种情况下,我尝试将render:action =>'rationale',:id => @edition更改为仅渲染,并对render动作进行了其他类似的调整.我只是不知道从哪里开始factory_girl帮助查看.任何帮助将不胜感激.
In this I've tried changing render :action => 'rationale', :id => @edition to just render, and other similar tweaks to the render action. I just have no idea where to start a factory_girl helped view. Any help would be greatly appreciated.
版本:
Rails 3.0.10
Rails 3.0.10
RSpec 2.7
RSpec 2.7
Factory_Girl 2.2
Factory_Girl 2.2
推荐答案
我认为错误是@editions
需要一个数组,因此您应该编写
I think the error is that @editions
is expecting an array, so you should write
assign(:editions, [@edition])
,否则,如果它应该是一个元素,则应编写:
or, otherwise, if it should be a single element, you should write:
assign(:edition, @edition)
第二,除非您的视图是期望变量的局部视图,否则您应该只写
Secondly, unless your view is a partial that expects variables, you should just write
render
您不必执行操作,rspec知道要从describe
渲染哪个视图,并且该视图不检索任何数据(因此您不必设置id),只需设置使用assigns
正确地实例变量.测试视图无非就是渲染视图.
You do not have to give the action, rspec knows which view to render from the describe
, and the view does not retrieve any data (so you don't have to set the id), you just have to set the instance variables correctly using the assigns
. Testing the view does nothing more than rendering the view.
希望这会有所帮助.
这篇关于在Rspec视图中使用Factory Girl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!