27

RSpecを使用してWindowsでRails3アプリケーションをテストしようとしました。テストとファクトリを作成しましたが、コマンドラインでRSpecを実行したときに発生する問題を解決できません。

テストファイルの1つは次のとおりです。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

そして、これが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

RSpecを実行しようとすると、次のエラーが発生します。

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

私は何が間違っているのですか?

4

4 に答える 4

67

これは古い質問ですが、「Trait notregistered」を検索したときに他の誰かがここにたどり着いた場合:

質問からファクトリでどのようemailに依存するかなどの依存属性を使用する場合、遅延評価されるように、属性を中括弧で囲む必要があります。name:club

email {"#{name}@example.com"}
于 2014-07-03T13:39:17.963 に答える
6

これはFactoryGirlエラーであり、(spec / features / sign_in_spec.rb:11で)次のようなものを使用しているようです。

FactoryGirl.create :user, :name

これは、ファクトリーの名前と呼ばれる特性を登録した場合にのみ機能します。user特性の詳細については、こちらをご覧ください

作成したユーザーの名前を上書きするだけの場合、構文は次のようになります。

FactoryGirl.create :user, name: 'THE NAME'
于 2013-01-07T00:35:11.320 に答える
3

将来の読者の参考のために:

何が機能しなかったのか -ArgumentError:トレイトが登録されていません: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. 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. user_idを他のブロック使用ヘルパーの下に移動します。
    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
于 2021-02-26T09:25:40.607 に答える
1

別の遅い答え。testモデルが非常に新しいことを忘れ、データベースを移行しなかったため、しばらく頭を悩ませました。したがって、属性は実際には存在しませんでした。

つまり、事前に実行する必要がありました

rails db:migrate RAILS_ENV=test
于 2021-12-23T12:45:10.793 に答える