我剛剛更新了我的 Gemfile。
一開始我認為問題來自Zeitwerk(from 2.4.2to 2.5.4) 但我已經降級了它并且我的規范仍然存在問題。我已經隔離問題不是來自RSpec依賴關系。
實際上,RSpec 沒有找到在另一個檔案中定義且與檔案名/類名不匹配的類。
重點: Filter::MyStandardError找到了。
# app/services/filter/my_standard_error.rb
module Filter
class MyStandardError < StandardError; end
class MySpecificError < MyStandardError; end
# ...
end
# app/services/filter/my_tested_service.rb
module Filter
class MyTestedService
def initialize
raise ::Filter::MySpecificError
end
end
end
RSpec.describe Filter::MyTestedService do
subject { described_class.new }
it 'raises an error'
expect{subject}.to raise_error(::Filter::MySpecificError)
end
end
我得到了錯誤:
NameError:
uninitialized constant Filter::MySpecificError
我得到了變更日志,但我的配置中沒有使用重大更改。
有人對這個有想法嗎?
uj5u.com熱心網友回復:
您編輯了描述。
這些是真實的檔案名、真實的內容和真實的類/模塊名嗎?還是他們是假的?
uj5u.com熱心網友回復:
您不需要添加app/services到自動加載路徑,這是由 Rails 自動完成的。我建議洗掉該配置以保持簡單/慣用。
app/services/filter.rb應該不需要執行。您的應用程式正在做一些不正確的事情,我們只需要找到它。
能否請您洗掉app/services/filter.rb,拋出Rails.autoloaders.log!,config/application.rb觸發錯誤,并分享痕跡?
uj5u.com熱心網友回復:
哦,描述又被編輯了!
您不能在同一檔案中的同一級別定義兩個常量。它是一個常數,一個檔案。這在 Zeitwerk 升級中沒有改變。標準錯誤需要一個檔案,特定錯誤需要另一個檔案。
uj5u.com熱心網友回復:
在同一頂層讀取一個檔案一個常量后
我發現這可以解決我的問題
# app/services/filter.rb
class Filter
class MyStandardError < StandardError; end
class MySpecificError < MyStandardError; end
end
# app/services/filter/my_tested_service.rb
class Filter
class MyTestedService
def initialize
raise ::Filter::MySpecificError
end
end
end
我仍然不知道為什么它以前作業..
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437156.html
