Michael Ef
Michael Ef

Reputation: 151

factory_girl: Strange error when creating objects

I get a strange error whenever I call any FactoryGirl create or build method.

In my spec file I have a before-block where I want to create a basic list of users:

require 'spec_helper'

describe "Users" do
  before do
    create_list(:user, 3, :confirmed)
    @user = User.first
  end
end

This line create_list throws the following error:

TypeError: nil is not a symbol
./spec/requests/users_spec.rb:5:in `block (2 levels) in <top (required)>'

My only factory file ./spec/factories/user.rb seems to be loaded. If I raise an error in the first line this error is thrown when I execute the spec tests.

This is the content of my factory file:

FactoryGirl.define do
  factory :user, aliases: [:friend] do
    sequence(:username) { |n| "foo#{n}" }
    password "foobar"
    password_confirmation {"#{password}"}
    email { "#{username}@example.com" }
    date_of_birth 20.years.ago

    trait :admin do
      admin true
    end
    trait :game_admin do
       game_admin true
    end
    trait :confirmed do
      confirmed_at Time.now
    end
  end
end

Upvotes: 3

Views: 680

Answers (1)

Michael Ef
Michael Ef

Reputation: 151

The problem is solved. After digging deeper into the full trace I found that the error was thrown in a part of friendly_id. friendly_id tried to work with the primary_key of my model User, which (however) returned nil. And so, User.send(key) throw this error.

I don't know why the primary_key of my model was null suddenly, but after the following rake commands everything is fine now:

rake db:drop
rake db:create
rake db:migrate
rake db:test:prepare

Upvotes: 2

Related Questions