将 Factory Girl 与 Rspec 视图结合使用

发布于 2024-12-17 01:12:32 字数 822 浏览 1 评论 0原文

所以我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-12-24 01:12:32

我认为错误是 @editions 期望一个数组,所以你应该写

assign(:editions, [@edition])

,否则,如果它应该是单个元素,你应该写:

assign(:edition, @edition)

其次,除非你的视图是期望的部分变量,你应该只写

render

你不必给出操作,rspec知道要从describe渲染哪个视图,并且该视图不会检索任何数据(因此你不必设置id),您只需使用正确设置实例变量分配。测试视图只不过是渲染视图而已。

希望这有帮助。

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

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.

Hope this helps.

对你而言 2024-12-24 01:12:32

现在您已经得到了答案,请停止编写视图规范。它们毫无必要地难以编写,而且它们实际上没有进行足够的测试,根本不值得。使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文