每次我为模型类Spec:
运行RSpec测试时,我都会收到此错误NoMethodError: undefined method `new' for Spec:Module
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/invocation_tracker.rb:11:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:19:in `block in initialize'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `instance_exec'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `build_class_instance'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:13:in `object'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/build.rb:9:in `result'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
./spec/models/spec_spec.rb:13:in `block (3 levels) in <top (required)>'
./spec/models/spec_spec.rb:16:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
我已经查看了spec模型类中是否存在任何预期的模型方法,并且找不到任何模型方法。
我可以使用&#39;规格&#39;作为模型名称,如果我正在使用RSpec或是否有解决方法?
spec.rb(Rails模型)
class Spec < ApplicationRecord
belongs_to :scenario
validates :scenario_id, presence: true
validates :description, presence: true, length: { minimum: 1, maximum: 100 }
validates :selected_section, presence: true, length: { maximum: 200 }
validates :selected_action, presence: true, length: { maximum: 200 }
end
spec_spec.rb
require 'rails_helper'
RSpec.describe Spec, type: :model do
context 'Relationship' do
it 'should belong to project' do
should belong_to(:scenario)
end
end
context 'Validations' do
let(:f) {FactoryGirl.build(:scenario)}
subject do
FactoryGirl.build(:spec, scenario: f)
end
it 'Correct input to be valid' do
expect(subject).to be_valid
end
context 'scenario_id, needs to be' do
it 'presence' do
should validate_presence_of(:feature_id)
end
end
context 'description, needs to be' do
it 'presence' do
should validate_presence_of(:description)
end
it 'minimum length of 1' do
should validate_length_of(:description).is_at_least(1)
end
it 'maximum length of 100' do
should validate_length_of(:description).is_at_most(100)
end
end
context 'selected_section, needs to be' do
it 'presence' do
should validate_presence_of(:selected_section)
end
it 'maximum length of 100' do
should validate_length_of(:selected_section).is_at_most(200)
end
end
context 'selected_action, needs to be' do
it 'presence' do
should validate_presence_of(:selected_action)
end
it 'maximum length of 100' do
should validate_length_of(:selected_action).is_at_most(200)
end
end
end
end
答案 0 :(得分:2)
正如您已经猜到的,您在Spec
课程与RSpec's Spec
module之间发现了命名空间冲突。
以下是解决问题的两种方法。
将您的类及其文件名重命名为Rails.root / models / the_spec.rb
class TheSpec < ApplicationRecord
belongs_to :scenario
# ... your code
end
命名空间,将其移至Rails.root / models / internal / spec.rb
class Internal::Spec < ApplicationRecord
belongs_to :scenario
# ... your code
end
您还需要编辑和移动/重命名spec_spec.rb
文件以匹配。