苦苦挣扎于RSpec 3.1,FactoryGirl 4.5和Rails 3.2
billing/plan_spec.rb
没问题,但每当我尝试运行billing/payment_spec.rb
时,uninitialized constant Billing::Payment::BillingPlan (NameError)
都会失败
任何想法为什么FactoryGirl正在寻找Billing::Payment::BillingPlan
而不是Billing::Plan
?
任何提示如何解决?
文件:
# factories\billing_plans.rb
FactoryGirl.define do
factory :billing_plan, :class => 'Billing::Plan' do
user
end
end
-
# factories\billing_payments.rb
FactoryGirl.define do
factory :billing_payment, :class => 'Billing::Payment' do
billing_plan
amount 1234
end
end
-
# billing/payment_spec.rb
require 'spec_helper'
describe Billing::Payment do
it 'has a valid factory' do
expect(build(:billing_payment)).to be_valid
end
it 'requires an amount' do
expect(build(:billing_payment, amount: nil)).to_not be_valid
end
end
-
# billing/plan_spec.rb
require 'spec_helper'
describe Billing::Plan do
it 'has a valid factory' do
expect(build(:billing_plan)).to be_valid
end
it 'requires an user' do
expect(build(:billing_plan, user: nil)).to_not be_valid
end
end
-
# billing/payment.rb
class Billing::Payment < ActiveRecord::Base
belongs_to :billing_plan
attr_accessible :amount, :billing_plan_id
validates :amount, presence: true
end
-
# billing/plan.rb
class Billing::Plan < ActiveRecord::Base
belongs_to :user
attr_accessible :user_id
validates :user, presence: true
end
答案 0 :(得分:1)
如果有其他人遇到这个问题,并尝试了上述建议而没有成功,那么我的所作所为:
我的模特:RewardSystem::AchievementTemplate
此测试的规范位于:/spec/models/reward_system/achievement_template_spec.rb
那么,如果您将spec文件中的模型名称更改为命名空间版本,那么它可以正常工作(假设您没有从config.infer_spec_type_from_file_location!
文件中删除rails_helper.rb
)
<强> FILES:强>
工厂/ achievement_template.rb:
FactoryGirl.define do
factory :achievement_template do
end
end
模型/ achievement_template_spec.rb
require 'rails_helper'
RSpec.describe RewardSystem::AchievementTemplate, type: :model do
context "Awarding" do
it "should award coins for unlocking an achievement" do
achievement = FactoryGirl.create(:reward_template)
end
end
end
答案 1 :(得分:0)
您是否尝试过:class => Billing::Plan
或:class => '::Billing::Plan'
?