加载程序未加载
问题描述:
我试图在我的规格中使用加工程序时收到空阵列。我的猜测是制造商文件尚未加载。如果我在RSpec初始化之后加载制造器文件,则会引发Fabrication::DuplicateFabricatorError
。这里是我的设置:加载程序未加载
- 轨4.1.4
- rspec的核心3.1.5
- RSpec的护栏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
得到一个空数组返回我的制作者后,我跑规格时出现此错误:undefined method 'email' for []:Array
。我希望获得通过的规格。
答
就我所见,您有两个问题。第一个是,你并不需要在你的rails_helper.rb
config.fixture_path = "#{::Rails.root}/spec/fabricators"
第二此行是你在你的规范的before
块调用Fabricator
代替Fabricate
。 Fabricator
用于声明定义,Fabricate
用于实际创建对象。
谢谢保罗!我的规范正在通过这两个变化。我因为错过了拼写差异而感到很傻,但现在你提到它,这个词的选择很有意义。 – Nathan 2014-10-08 16:06:51