![](/img/trans.png)
[英]I am having this issue, 'NameError: uninitialized constant Profile::friend', when running 'rake db:seed'
[英]Getting uninitialized constant (NameError) inside decorators when running rspec tests via rake
在我的應用程序中,我使用了一個引擎(blogit),我想添加一些更改/行為。
我按照指南介紹了如何覆蓋引擎控制器/模型並添加了以下內容:
在config/initializer/blogit.rb
# Requires extension ruby files in lib/blogit.
Dir[Rails.root.join("lib/blogit/*.rb")].each {|f| require f}
在lib/blogit/engine.rb
module Blogit
class Engine < ::Rails::Engine
isolate_namespace Blogit
config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/blogit/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end
在app/decorators/controllers/blogit/comments_controller_decorator.rb
Blogit::CommentsController.class_eval do
def create
Rails.logger.info "decorated controller action"
# ... overridden stripped ...
end
end
在app/decorators/models/blogit/comment_decorator.rb
Blogit::Comment.class_eval do
belongs_to :user
end
要提到的:
如果我運行rake
我會得到一個奇怪的錯誤:
/Users/Kassi/.rvm/rubies/ruby-1.9.3-p392-railsexpress/bin/ruby -S rspec ./spec/controllers/home_controller_spec.rb ./spec/models/user_spec.rb
/Users/Kassi/demo/app/decorators/controllers/blogit/comments_controller_decorator.rb:3:in `<top (required)>': uninitialized constant Blogit::CommentsController (NameError)
但是,如果我運行rake手動提到的第一行( .../ruby -S ...
),則所有測試都正在成功運行。
在我的項目,我用后衛 叉勺 。 跑衛也會讓測試通過而沒有任何錯誤。
應用程序本身運行良好,即它啟動沒有錯誤,我能夠按我的意願發表評論。 我的裝飾者動作代碼正在執行。
那么跑耙時有什么不同? 它為什么會破裂?
注意:
require "blogit"
或require "blogit/comments_controller"
沒有幫助。 它實際上找不到使用require的控制器。 由於這個問題是一個更大的項目的一部分,我從頭開始創建一個新的應用程序用於測試目的,只包含相關的東西:基本的rails應用程序,rspec,設計,blogit和裝飾器。 它可以在這里找到: https ://github.com/kassi/decorator_demo_rspec(git://github.com/kassi/decorator_demo_rspec.git)
另一個使用testunit(工作正常!)的repo可以在這里找到: https ://github.com/kassi/decorator_demo_testunit(git://github.com/kassi/decorator_demo_testunit.git)
您可以嘗試使用為其執行此操作的activesupport-decorators ,而不是自己執行require語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.