:FactoryGirl는 열거 상태 필드에 잘못된 값을 제공합니다 나는 Client.rb 모델에 대한 FactoryGirl 공장 구축을 위해 노력하고
Client.rb
enum status: [ :unregistered, :registered ]
has_many :quotation_requests
#Validations
validates :first_name,
presence: true,
length: {minimum: 2}
validates :last_name,
presence: true,
length: {minimum: 2}
validates :email, email: true
validates :status, presence: true
공장 :
FactoryGirl.define do
factory :client do
first_name "Peter"
last_name "Johnson"
sequence(:email) { |n| "peterjohnson#{n}@example.com" }
password "somepassword"
status "unregistered"
end
end
client_spec을 .rb
require 'rails_helper'
RSpec.describe Client, type: :model do
describe 'factory' do
it "has a valid factory" do
expect(FactoryGirl.build(:client).to be_valid
end
end
end
다음 오류가 발생합니다.
1) Client factory has a valid factory
Failure/Error: expect(FactoryGirl.build(:client, status: 'unregistered')).to be_valid
expected #<Client id: nil, email: "[email protected]", encrypted_password: "$2a$04$urndfdXNfKVqYB5t3kERZ.c.DUitIVXEZ6f19FNYZ2C...", first_name: "Peter", last_name: "Johnson", status: "0", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Status can't be blank
오류는 상태를 비워 둘 수 없습니다.
공장에서 상태 속성에 값을 명확하게 지정했기 때문에 이것이 가능한지 이해할 수 없습니다.
이 팩토리에서 유효한 클라이언트 객체를 만들려면 어떻게해야합니까?
는 factory_girl 4.7.0 사용 factory_girl_rails를 사용하여 4.2 레일 4.7.0
let (: client)을 사용하도록 코드가 변경되었지만 아무 것도 변경되지 않았습니다. 나는 클라이언트를 생성하지 않고 공장이 유효한 클라이언트를 빌드하는지 테스트하려고합니다. 그렇게하면 다른 테스트를 위해 공장을 정확하게 사용할 수 있습니다. 나는 상태를 시도했다 : 등록되지 않은 상태 0 # : 등록이 해제되어 있고 할당 된 상태가 표시 되더라도 여전히 '상태를 비워 둘 수 없음'이라는 오류 메시지가 표시된다. – chell