【问题标题】:Fabricators not loading制造商不加载
【发布时间】: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


    【解决方案1】:

    据我所知,这里有两个问题。首先是你的rails_helper.rb中不需要这行

    config.fixture_path = "#{::Rails.root}/spec/fabricators"
    

    第二个是您在规范的before 块中调用Fabricator 而不是FabricateFabricator 用于声明定义,Fabricate 用于实际创建对象。

    【讨论】:

    • 谢谢保罗!我的规范随着这两个变化而通过。我因为错过了拼写差异而感到很傻,但是既然你提到它,这个词的选择就很有意义了。
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多