LoginSignup
3
3

More than 5 years have passed since last update.

FactoryGirlでクラス名と違う名前で定義

Posted at

FactoryGirlでマヌケなハマり方してたのでメモ

spec/factories/users.rb
FactoryGirl.define do
  factory :user do
    email 'hoge@fuga.jp'
    name 'Taro Yamada'
    age 32
  end

  factory :user_email_only, class: User do
    email 'hoge@fuga.jp'
  end
end

クラス名と同じときは省略できる

factory :user do

クラス名と違うときはクラス名を記述

factory :user_email_only, class: User do

はじめはクラス名を指定しておらず、定義したはずのfactoryが呼べなくてしばらく悩んだ。

factory :user_email_only do
  email 'hoge@fuga.jp'
end
it 'only email' do
  post :create user: attributes_for(:user_email_only)
  expect(response).to redirect_to(edit_user_path)
end
Failure/Error: user_attributes = attributes_for(:user_email_only)
NameError:
  uninitialized constant UserEmailOnly

単にクラス名指定してなかっただけ。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3