2014-10-07 105 views
0

我試圖在我的規格中使用加工程序時收到空陣列。我的猜測是製造商文件尚未加載。如果我在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。我希望獲得通過的規格。

回答

3

就我所見,您有兩個問題。第一個是,你並不需要在你的rails_helper.rb

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

第二此行是你在你的規範的before塊調用Fabricator代替FabricateFabricator用於聲明定義,Fabricate用於實際創建對象。

+0

謝謝保羅!我的規範正在通過這兩個變化。我因爲錯過了拼寫差異而感到很傻,但現在你提到它,這個詞的選擇很有意義。 – Nathan 2014-10-08 16:06:51