我正在創建一個簡單的Ruby Gem,它目前按照Making your own gem文檔中的示例進行佈局。需要在Ruby Gem中進行rspec測試的路徑
我的目錄結構:
.
├── Gemfile
├── Gemfile.lock
├── lib
│ ├── go_run
│ │ ├── parser.rb
│ │ └── runner.rb
│ └── go_run.rb
└── spec
├── go_run_spec.rb
├── parser_spec.rb
└── runner_spec.rb
我原來叫Runner
類lib/go_run/runner.rb
只是Runner
,但現在,按照所記錄的例子中,我GoRun::Runner
命名空間下它。代碼起作用。問題是現在運行的bundle exec rspec spec/parser_spec.rb
沒有。它失敗:
/home/smurf/dev/ruby/go_run/lib/go_run/parser.rb:3:in `<top (required)>': uninitialized constant GoRun (NameError)
這個文件的第3行:
class GoRun::Parser
我require
使用require 'go_run/parser'
荷蘭國際集團的庫代碼中spec/parser_spec.rb
。
我試着用require_relative '../lib/go_run/parser'
來包括它,但是產生了同樣的錯誤。
有人知道我在做什麼錯嗎?
更新:我已經上傳的完整代碼在這個分支:https://github.com/henrytk/go_run/tree/stackoverflow-43155117
Thanks @Drenmi。我只想在'GoRun :: Parser'下命名空間,因爲這是文檔如何做的,你知道它爲什麼這樣做嗎?將它作爲'Parser'離開它會帶來什麼樣的危害? – HenryTK
您需要考慮您的寶石將與其他庫和用戶代碼一起生活。如果他們或任何其他寶石(如RuboCop)對Parser gem具有依賴性,則會產生命名衝突。 :-) – Drenmi
非常好。我的Gem只能用於可執行的'run',而不是庫,所以我不希望任何人在他們的代碼中實際需要'go_run'',只需'gem install'並使用它。感謝您的幫助。 – HenryTK