簡體   English   中英

NameError:未初始化的常量CreatesPointOfInterest Rspec

[英]NameError: uninitialized constant CreatesPointOfInterest Rspec

我正在閱讀《 Rails 5測試處方》這本書,但在運行其中一個示例時遇到問題。 謝謝你的幫助。

我確實有一個名為point_of_interest.rb的模型app / models / point_of_interest.rb

我收到以下錯誤:

 NameError: uninitialized constant CreatesPointOfInterest

規范/工作流程/creates_point_of_interest.rb

class CreatesPointOfInterest
  attr_accessor :name, :point_of_interest

  def initialize(name: "")
   @name = name
  end

  def build
   self.point_of_interest = PointOfInterest.new(name: name)
  end
end

規范/工作流程/creates_point_of_interest_spec.rb

 require "rails_helper"
RSpec.describe CreatesPointOfInterest do
 describe "initialization" do
   it "creates a point of interest given a name" do
     creator = CreatesPointOfInterest.new(name: "Pyramids of Giza")
     creator.build
     expect(creator.point_of_interest.name).to eq("Pyramids of Giza")
   end
 end
end

終端輸出:

〜/桌面/冒險/冒險[開發|!SP] $ rspec

An error occurred while loading 
./spec/workflows/creates_point_of_interest_spec.rb.
Failure/Error:
  RSpec.describe CreatesPointOfInterest do

   describe "initialization" do
        it "creates a point of interest given a name" do
            creator = CreatesPointOfInterest.new(name: "Pyramids of 
            Giza")
            creator.build
            expect(creator.point_of_interest.name).to eq("Pyramids of 
           Giza")
         end
   end


 NameError:
   uninitialized constant CreatesPointOfInterest

其他錯誤:

    .rvm/gems/ruby-2.3.3/gems/bootsnap- 
    1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in 
   `block in load_missing_constant'
    .rvm/gems/ruby-2.3.3/gems/bootsnap- 
    1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in 
    `without_bootsnap_cache'
    .rvm/gems/ruby-2.3.3/gems/bootsnap- 
    1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in 
    `rescue in load_missing_constant'
    .rvm/gems/ruby-2.3.3/gems/bootsnap- 
    1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in 
    `load_missing_constant'
    ./spec/workflows/creates_point_of_interest_spec.rb:3:in `<main>'
    .rvm/gems/ruby-2.3.3/gems/bootsnap- 
   1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in 

 --- Caused by: ---
  NameError:  uninitialized constant CreatesPointOfInterest

您的應用使用的類應位於app/目錄中,而不是spec

因此,您應該具有:

app/workflows/creates_point_of_interest.rb

spec/workflows/creates_point_of_interest.rb

您的規范文件名應與類名匹配。 您確定這是正確的嗎?

spec/workflows/create_point_of_interest_spec.rb

但這應該是

spec/workflows/creates_point_of_interest_spec.rb

暫無
暫無

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

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