名称错误:RSpec 中未初始化的常量解析器



我正在尝试在我的 Ruby 2.5.0 应用程序中测试简单的类:

source/parsers/jira_parser.rb

module Parsers
class JiraParser
def initialize(event)
payload = event['body']
@event = JSON.parse(payload)
end
def call
{
reporter_email: reporter_email,
reporter_name: reporter_name,
ticket_number: ticket_number,
description: description
}
end
private
attr_reader :event
def reporter_email
event.dig('issue', 'fields', 'reporter', 'emailAddress')
end
# other methods from call are pretty much the same as `reporter_email`

规格如下:

spec/source/parsers/jira_parser_spec.rb

require 'spec_helper'
RSpec.describe Parsers::JiraParser do
describe 'call' do
subject(:hash_creation) { described_class.new(event).call }
let(:reporter_name) { 'john.doe' }
let(:reporter_email) { 'john.doe@example.com' }
let(:description) { 'This is a test description' }
let(:event) do
{
'body' => {
'issue': {
'key': 'TEST-Board-123',
'fields': {
'reporter': {
'displayName': reporter_name,
'emailAddress': reporter_email
},
'description': description
}
}
}
}
end
it { expect(hash_creation).to be_success }
end
end

但是我有一个错误:

名称错误: 未初始化的常量解析器

./spec/source/parsers/jira_parser_spec.rb:5:in '' 未找到示例。

我应该在我的rspec_helper中添加一些东西来使其正常工作吗? 现在它非常基本:

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end

我知道这只是Ruby,没有Rails,因此没有魔法。您需要在规范文件中提供一个源文件,因此您必须在顶部放置以下内容:

require_relative '../../../source/parsers/jira_parser

您好,我还是新手,所以这可能会有所帮助,但我相信您需要 JiraParser

require 'jira_parser'
require 'jira_parser/parser'

这可能有效,但错误是因为您尝试使用当前代码中无法访问的解析器。

好的,我想通了 - 我所要做的就是在.rspec文件中添加-I source以加载所有测试的类。所以就我而言.rspec如下所示:

.rspec

--require spec_helper
-I source

最新更新