スペックでファブリケーターを使用しようとすると、空の配列が表示されます。私の推測では、ファブリケーター ファイルが読み込まれていません。RSpec の初期化後にファブリケーター ファイルをロードすると、aFabrication::DuplicateFabricatorError
が発生します。これが私のセットアップです:
- レール4.1.4
- rspec コア 3.1.5
- rspec-rails 3.1.0
- 製作 2.11.3
.
# config/application.rb
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :fabrication, dir: "spec/fabricators"
end
# spec/rails_helper.rb
config.fixture_path = "#{::Rails.root}/spec/fabricators"
config.use_transactional_fixtures = true
動作するはずのコードは次のとおりです。
# app/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
# spec/fabricators/user_fabricator.rb
Fabricator(:user) do
email { Faker::Internet.email }
password "password"
password_confirmation "password"
end
# spec/models/user_spec.rb
require 'rails_helper'
describe User do
before :each do
@user = Fabricator(:user)
#=> []
end
it "has an email" do
expect(@user.email).to be_a(String)
expect(@user.email.length).to be > 0
end
end
ファブリケーターに空の配列が返された後、specs: を実行するとこのエラーが発生しますundefined method 'email' for []:Array
。合格スペックを期待しています。