未初始化的恒永

时间:2018-02-12 07:57:09

标签: ruby-on-rails rspec

当我尝试使用Rspec和Factory girl运行模型的测试用例时,它显示未初始化的常量Wing  工厂中的代码/ wing.rb

FactoryGirl.define do 
  factory :wing do
    wing_name "Example Title"
    is_deleted "0"
    mg_school_id "1"
    created_by "2013-06-02 02:28:12"
    updated_by "2013-06-02 02:28:12"
  end 
end

model / mg_wing_spec.rb中的代码

require 'rails_helper'

RSpec.describe MgWing, type: :model do
  it "has a valid factory" do
    @hai=FactoryGirl.create(:wing)
  end
end

2 个答案:

答案 0 :(得分:0)

将文件名factories/wing.rb更改为factories/mg_wing.rb

您的工厂名称应与您的型号和rspec文件名相同。

如果您需要,可以使用以下几个教程:

https://medium.com/@JonoYeong/setting-up-rspec-and-factory-girl-8cf287801099

https://semaphoreci.com/community/tutorials/working-effectively-with-data-factories-using-factorygirl

答案 1 :(得分:0)

您需要与模型名称相同的工厂名称或传递class参数并且factorygirl将负责它

解决方案1 ​​

FactoryGirl.define do 
  factory :mg_wing do
    ...
  end 
end

解决方案2

FactoryGirl.define do 
  factory :wing, class: MgWing do
    ...
  end 
end