RSpec gives error 'trait not registered: name'

六月ゝ 毕业季﹏ 提交于 2019-12-06 16:58:57

问题


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 "test@example.com"
  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?


回答1:


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"}



回答2:


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'


来源:https://stackoverflow.com/questions/14188273/rspec-gives-error-trait-not-registered-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!