使用具有Rspec视图的Factory Girl

Eri*_*c C 2 rspec ruby-on-rails rspec-rails factory-bot

所以我想使用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
Run Code Online (Sandbox Code Playgroud)

在这个我尝试改变渲染:action =>'reasonale',:id => @edition来渲染,以及对渲染动作的其他类似调整.我只是不知道从哪里开始一个factory_girl帮助查看.任何帮助将不胜感激.

版本:

Rails 3.0.10

RSpec 2.7

Factory_Girl 2.2

nat*_*vda 6

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

assign(:editions, [@edition])
Run Code Online (Sandbox Code Playgroud)

或者,如果它应该是单个元素,你应该写:

assign(:edition, @edition)
Run Code Online (Sandbox Code Playgroud)

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

render
Run Code Online (Sandbox Code Playgroud)

你不必给出动作,rspec知道从哪个视图渲染describe,并且视图不检索任何数据(所以你不必设置id),你只需要正确设置实例变量assigns.测试视图只不过是渲染视图.

希望这可以帮助.