모델을 만들 때마다 FactoryGirl을 사용하고 고유 속성을 채 웁니다. 내 모델 form
의 문제는 form_type
속성에 대해 4 가지 유형 만 사용할 수 있다는 것입니다. 따라서 테스트를 실행할 때마다 sequence
을 재설정해야합니다. 아래에 나와있는 것처럼 사용자 before do
은 FactoryGirl.reload
으로 전화를 겁니다. 그러나 Factory Girl의 반 패턴이라고 말하는 기사를 보았습니다. 모든 테스트 전에 FactoryGirl.reload
을 호출하는 대신 FactoryGirl에서 시퀀스를 재설정하는 가장 좋은 방법은 무엇입니까? 여기 모든 테스트 전에 FactoryGirl에서 순서를 재설정하는 올바른 방법
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 파일입니다 : 여기
여기
FactoryGirl.define do
factory :form do
association :user
sequence :form_type do |n|
Form.form_types.values[n]
end
end
end
내 form.rb 모델 파일입니다, 내
forms.rb
Factorygirl 파일입니다
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