所以我想使用Rspec和Factory Girl测试一些视图,但我不知道如何正确地将工厂分配给视图。我在网上看到的所有信息都使用存根,虽然存根很好,但我想让我的rspec内容保持一定的一致性。
我想为版本模型使用工厂,并在呈现页面时将其与页面相关联,但我所考虑的一切似乎都是错误的。这是一个有点可笑的例子:
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更改为just render,并对render操作进行了其他类似的调整。我只是不知道从哪里开始factory_girl帮助视图。任何帮助都将不胜感激。
版本:
Rails 3.0.10
RSpec 2.7
Factory_Girl 2.2
发布于 2011-11-19 05:39:38
我认为错误是@editions需要一个数组,所以你应该写
assign(:editions, [@edition])否则,如果它应该是单个元素,则应编写:
assign(:edition, @edition)其次,除非您的视图是需要变量的部分视图,否则您应该只编写
render您不必提供操作,rspec知道从describe呈现哪个视图,并且视图不检索任何数据(因此您不必设置id),您只需使用assigns正确设置实例变量。测试视图只不过是渲染视图而已。
希望这能有所帮助。
发布于 2011-11-29 06:02:58
现在你已经得到了答案,停止编写视图规范。它们不必要地很难编写,而且它们真的没有足够的测试来证明它们是值得的。使用Cucumber进行视图和控制器测试。
https://stackoverflow.com/questions/8187944
复制相似问题