簡體   English   中英

NameError:RSpec 中未初始化的常量解析器

[英]NameError: uninitialized constant Parsers in 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`

具有以下規格:

規范/源/解析器/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

但我有一個錯誤:

NameError:未初始化的常量解析器

./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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM