将 Factory Girl 与 Rspec 视图结合使用
所以我想使用 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 => '基本原理', :id => @edition 仅渲染,以及对渲染操作的其他类似调整。我只是不知道从哪里开始 Factory_girl 帮助查看。任何帮助将不胜感激。
版本:
Rails 3.0.10
RSpec 2.7
Factory_Girl 2.2
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
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.
Versions:
Rails 3.0.10
RSpec 2.7
Factory_Girl 2.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为错误是
@editions
期望一个数组,所以你应该写,否则,如果它应该是单个元素,你应该写:
其次,除非你的视图是期望的部分变量,你应该只写
你不必给出操作,rspec知道要从
describe
渲染哪个视图,并且该视图不会检索任何数据(因此你不必设置id),您只需使用正确设置实例变量分配
。测试视图只不过是渲染视图而已。希望这有帮助。
I think the error is that
@editions
is expecting an array, so you should writeor, otherwise, if it should be a single element, you should write:
Secondly, unless your view is a partial that expects variables, you should just write
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 theassigns
. Testing the view does nothing more than rendering the view.Hope this helps.
现在您已经得到了答案,请停止编写视图规范。它们毫无必要地难以编写,而且它们实际上没有进行足够的测试,根本不值得。使用 Cucumber 进行视图和控制器测试。
Now that you've got the answer, stop writing view specs. They are needlessly hard to write, and they really don't test enough to be at all worthwhile. Do your view and controller testing with Cucumber instead.