Search code examples
ruby-on-rails-3rspecfactory-bot

RSpec gives error 'trait not registered: name'


I tried to test my Rails 3 application on Windows with RSpec. I've wrote tests and factories, but can't solve the issues which raise when I run RSpec on command line.

Here is one of the test files: require 'spec_helper'

describe "SignIns" do
  it "can sign in" do
    user = FactoryGirl.create(:user)
    visit new_user_session_path
    fill_in "login", with: user.username
    fill_in "password", with: user.password
    click_on "sign in"
    current_user.username.should == user.username
  end
end

And here's the factories.rb:

factory :layout do
  name "layout1"
end

factory :club do
  sequence(:name) { |i| "Club #{i}" }
  contact_name "John Doe"
  phone "+358401231234"
  email "#{name}@example.com"
  association :layout
end

factory :user do
  sequence(:username) { |i| "user#{i}" }
  password 'password'
  email "[email protected]"
  club
end

When I try to run RSpec it gives the following error:

trait not registered: name
  #C: in 'object'
  #.spec/features/sign_in_spec.rb:11:in 'block (2 levels) in (top(required))

What am I doing wrong?


Solution

  • I know this is an old question, but in case anyone else ends up here when searching "Trait not registered":

    When using a dependent attribute like how email depends on name in the :club factory from the question, you need to wrap the attribute in curly braces so it gets lazy evaluated:

    email {"#{name}@example.com"}