ruby on rails - Factory Girl associations undefined method for what's supposed to be an association -
i'm trying create error 2 factorygirl models. have cars , car manufacturers. cars belong car manufacturers foreign key/variable belongs in car. when run tests, error.
failure/error: car = factorygirl.build(:car) nomethoderror: undefined method `car_manufacturer=' #<car:0x0000010437a7d0>
here factories located in spec/factories folder
factorygirl.define factory :car color 'black' year 2012 mileage 50000 description 'badass used car' car_manufacturer end end factorygirl.define factory :car_manufacturer name 'speed racer inc.' country 'japan' end end
i don't have set in associations validations because understood, factory girl seperate should work. perhaps wrong in spec:
scenario 'i want associate car car manufacturer' car_manufacturer = factorygirl.create(:car_manufacturer) car = factorygirl.build(:car) car_count = car.count visit new_car_path fill_in 'color', with: car.color select car.year, from: 'year' fill_in 'mileage', with: car.mileage fill_in 'description', with: car.description select car_manufacturer.name, from: 'owner' click_on 'create car' expect(page).to have_content('car submitted') expect(car.count).to eql(car_count + 1) end
thank help. i'm unsure why happen , don't know should try fix it.
ok, think found answer. before didn't have associations in model file. have since added them below:
require 'spec_helper' describe car { should validate_presence_of(:color) } { should validate_presence_of(:year) } { should validate_presence_of(:mileage) } { should ensure_inclusion_of(:year).in_array(car::years) } { should_not have_valid(:year).when('','nineteenninetynine',1979) } { should_not have_valid(:mileage).when('3300 miles') } { should belong_to(:car_manufacturer) } end require 'spec_helper' describe carmanufacturer { should validate_presence_of(:name) } { should validate_presence_of(:country) } { should_not have_valid(:name).when('',nil) } { should_not have_valid(:country).when('',nil) } { should have_many(:cars) } end
my new question is, factory girl depend on model validations? under impression didn't. can clarify. tried github documentation don't remember explicitly said so. thx.
can verify you've migrated test database? often, when accessor isn't working correctly (in example, car_manufacturer=
), it's either spelled incorrectly or test database hasn't been migrated correctly (since ar inflect on table).
Comments
Post a Comment