【发布时间】:2014-10-07 20:10:35
【问题描述】:
在我的规范中尝试使用制造商时,我得到了一个空数组。我的猜测是制造商文件尚未加载。如果我在 RSpec 初始化后加载制造商文件,则会引发 Fabrication::DuplicateFabricatorError。这是我的设置:
- 导轨 4.1.4
- rspec-core 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
在为我的制造商返回一个空数组后,我在运行规范时收到此错误:undefined method 'email' for []:Array。我希望能通过规范。
【问题讨论】:
标签: ruby-on-rails-4 rspec3 fabrication-gem