Rspec错误未初始化的常数

时间:2018-07-11 15:29:21

标签: ruby-on-rails rspec

我有一家公司要做的招聘任务,涉及Rspec。克隆了我必须处理的存储库后,我立即遇到了一些错误。我实际上是Rspec的新手,或者实际上是测试的新手,我想改进一下,但是这个错误是我找不到解决方法。

spec / validators / title_brackets_validator_spec.rb

require "rails_helper"

describe TitleBracketsValidator do
  subject { Validatable.new(title: title) }

  shared_examples "has valid title" do
    it "should be valid" do
      expect(subject).to be_valid
    end
  end

  context "with curly brackets" do
    let(:title) { "The Fellowship of the Ring {Peter Jackson}" }
    it_behaves_like "has valid title"
  end

[ more not important 'contexts'...]
end

class Validatable
  include ActiveModel::Validations
  validates_with TitleBracketsValidator
  attr_accessor :title

  def initialize(title:)
    @title = title
  end
end

在运行bundle exec rspec时出现错误:

An error occurred while loading ./spec/validators/title_brackets_validator_spec.rb.
Failure/Error:
  describe TitleBracketsValidator do
    subject { Validatable.new(title: title) }

    shared_examples "has valid title" do
      it "should be valid" do
        expect(subject).to be_valid
      end
    end

    shared_examples "has invalid title" do

NameError:
  uninitialized constant TitleBracketsValidator
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `block in load_missing_constant'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `rescue in load_missing_constant'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in `load_missing_constant'
# ./spec/validators/title_brackets_validator_spec.rb:3:in `<top (required)>'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
# ------------------
# --- Caused by: ---
# NameError:
#   uninitialized constant TitleBracketsValidator
#   /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in `load_missing_constant'


Finished in 0.00045 seconds (files took 1.57 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Coverage report generated for RSpec to /home/maciej/Templates/task_app/coverage. 0 / 108 LOC (0.0%) covered.

1 个答案:

答案 0 :(得分:2)

仔细阅读作业-也许您的任务之一就是添加文件并实现类TitleBracketsValidator,以便它通过所有提供的测试?

我会在您的资源中搜索该课程。如果不存在-也许您应该创建一个。如果在那里-那么就是加载问题。 require按照您的规格进行操作,并通知招聘人员您已修复了可能的错误(可能会加分)