Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 384
Roope Hakulinen Avatar asked Jan 07 '13 00:01

Roope Hakulinen


3 Answers

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"}
like image 90
calvin Avatar answered Nov 18 '22 17:11

calvin


It's a FactoryGirl error, and it seems you're using (at spec/features/sign_in_spec.rb:11) something like :

FactoryGirl.create :user, :name

This will only work if you registered a trait called name for the Factory user, more on traits here

Note that if you just want to override the name of the created user, the syntax is

FactoryGirl.create :user, name: 'THE NAME'
like image 7
pjam Avatar answered Nov 18 '22 18:11

pjam


For future readers' reference:

What didn't work - ArgumentError: Trait not registered: user_id

    FactoryBot.define do
      factory :startup do
        user_id
        name { FFaker::Lorem.word }
        website { FFaker::Internet.uri(host: 'example.com') }
        founded_at { "01.01.2000" }
      end
    end

How I solved this issue using either of these, when everything seemed to look right:

  1. put empty curly braces after user_id
    FactoryBot.define do
      factory :startup do
        user_id {}
        name { FFaker::Lorem.word }
        website { FFaker::Internet.uri(host: 'example.com') }
        founded_at { "01.01.2000" }
      end
    end
  1. Move user_id below other block-using helpers:
    FactoryBot.define do
      factory :startup do
        name { FFaker::Lorem.word }
        website { FFaker::Internet.uri(host: 'example.com') }
        founded_at { "01.01.2000" }

        user_id
      end
    end
like image 4
LE-HU Avatar answered Nov 18 '22 18:11

LE-HU