0

必须使用FactoryGirl.reload可能会增加运行所有测试所需的时间(当存在许多测试时),并且它也被描述为Anti-Pattern

如何继续使用唯一字段的排序,同时测试正确的值?

你可以在我的代码中看到...

  • 我必须打电话FactoryGirl.reload,以便增量开始,1以防任何先前的测试已经运行。

  • 我必须说明,:count => 1因为该值只会有一个实例。

规格/工厂/clients.rb

FactoryGirl.define do
  factory :client do
    name                "Name"
    sequence(:email}    { |n| "email#{n}@example.com" }
    sequence(:username) { |n| "username#{n}" }
  end
end

规范/客户端/index.html.erb_spec.rb

require 'spec_helper'

describe "clients/index" do
  before do
    FactoryGirl.reload
    (1..5).each do
      FactoryGirl.create(:client)
    end
    @clients = Client.all
  end

  it "renders a list of clients" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
    assert_select "tr>td", :text => "Name".to_s, :count => 5
    assert_select "tr>td", :text => "email1@example.com".to_s, :count => 1
    assert_select "tr>td", :text => "username1".to_s, :count => 1
  end
end
4

2 回答 2

2

为这个特定的测试传递特殊名称怎么样?

require 'spec_helper'

describe "clients/index" do
  before do
    (1..5).each do |num|
      FactoryGirl.create(:client, username: "special#{num}")
    end
    @clients = Client.all
  end

  it "renders a list of clients" do
    render
    assert_select "tr>td", :text => "special1".to_s, :count => 1
  end
end
于 2013-04-17T02:13:38.747 回答
1

添加到 mind.blank 的答案...如果您希望测试增量值,请将它们也放在增量循环中。

  it "renders a list of clients" do
    render
    (1..5).each do |num|
      assert_select "tr>td", :text => "special#{num}".to_s, :count => 1
    end
  end
于 2013-04-17T14:05:35.647 回答