私は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