2016-07-25 23 views
1

私はFactoryGirlを使用しており、モデルが作成されるたびに固有の属性を設定しています。私のモデルformの問題は、form_type属性のために利用可能な4つの異なるタイプしかないということです。だから、テストを実行するたびにsequenceをリセットする必要があります。以下のように、私はユーザbefore doブロックFactoryGirl.reloadを呼び出します。しかし、私はFactoryGirlに反パターンであると言う記事を見ました。すべてのテストの前にFactoryGirl.reloadを呼び出すのではなく、FactoryGirlでシーケンスをリセットする最良の方法は何ですか?ここですべてのテストの前にFactoryGirlでシーケンスをリセットする適切な方法

はここ

FactoryGirl.define do 
    factory :form do 
    association :user 
     sequence :form_type do |n| 
      Form.form_types.values[n] 
     end 

    end 
end 

、私のforms.rb Factorygirlファイルである私のform.rbモデルファイルされる:

require 'rails_helper' 

RSpec.describe FormsController, type: :controller do 

    login_user 

    let(:form) { 
     FactoryGirl.create(:form, user: @current_user) 
    } 

    let(:forms) { 
     FactoryGirl.create_list(:form , 3, user: @current_user) 

    } 

    let(:form_attributes) { 
     FactoryGirl.attributes_for(:form, user: @current_user) 
    } 

    describe "GET #index" do 
     before do 
      FactoryGirl.reload 
     end 

     it "loads all of the forms into @forms" do 
      get :index 
      expect(assigns(:forms)).to match_array(@forms) 
     end 
    end 

end 

答えて

0

フム:

class Form < ActiveRecord::Base 
    belongs_to :user, required: true 

    enum form_types: { :a => "Form A", :b => "Form B", :c => "Form C", :d => "Form D"} 

    validates :form_type, presence: true 
    validates :form_type, uniqueness: {scope: :user_id} 

end 

ここでは私のforms_controller_spec.rbファイルですFGシーケンスの目的はユニークな番号を確保することにあるようです。それとも少なくとも私はそれを使っています。これが本当に必要な場合は、FGをハックすることができます。

この質問は役に立ちます。

How can I reset a factory_girl sequence?

関連する問題