I have the following model:
class Team < ActiveRecord::Base
What is being tested:
describe Team do before(:each) do @attr = FactoryGirl.attributes_for(:team) end it "should belong to a user" do @team = Team.create!(@attr) @team.should belong_to(:user) end end
And I have the following factories:
FactoryGirl.define do factory :team do name 'Dream Team' sport user end end FactoryGirl.define do factory :user do name 'Test User' last_name 'Last Name' email ' example@example.com ' password 'changeme' password_confirmation 'changeme' end end
When I test the specification, I get the following failure:
1) The team must belong to the user Error / Error: @team = Team.create! (@Attr) ActiveRecord :: StatementInvalid: SQLite3 :: ConstraintException: teams.user_id cannot be NULL: INSERT INTO "teams" ("created_at", "name", "sport_id", "updated_at", "user_id") VALUES ( ?,?,?,?,?)
Why? The docs say that to set up an association, you can just write the name Factory, in my case this is a user.
thanks
source share