子文件夹中未初始化的常量文件
uninitialized constant file in subfolder
我的测试文件夹结构如下:
test
models
restaurant
helpers
employee.rb
points_test.rb
所以我的 PointsTest
看起来像这样:
require 'models/restaurant/helpers/employee.rb'
class Restaurant::PointsTest < ActiveSupport::TestCase
....
employee1 = Restaurant::Employee.create
文件 Employee
是这样的:
class Restaurant::Employee
def self.create
.....
一切都是这样
---------------------------------------- -----
现在我尝试将 class Restaurant::Employee
更改为 class Restaurant::Helpers::Employee
并且在 PointsTest
中我更改为 employee1 = Restaurant::Helpers::Employee.create
然后我得到 这个错误:
未初始化常量 Restaurant::Helpers (NameError)
我做错了什么?我的意思是 Helpers
在子文件夹 helpers
!
中
您不能像 A::B::C::D
那样只 声明 链中的常量,因为要查找每个常量。
module Restaurant
module Helper # declare module Helper
class Employee
...
会起作用。而
# ⇓⇓⇓⇓⇓⇓ fail on try to const_get(:Helper)
class Restaurant::Helper::Employee
常量 Helper
查找失败,因为它未定义。希望对你有帮助。
我的测试文件夹结构如下:
test
models
restaurant
helpers
employee.rb
points_test.rb
所以我的 PointsTest
看起来像这样:
require 'models/restaurant/helpers/employee.rb'
class Restaurant::PointsTest < ActiveSupport::TestCase
....
employee1 = Restaurant::Employee.create
文件 Employee
是这样的:
class Restaurant::Employee
def self.create
.....
一切都是这样
---------------------------------------- -----
现在我尝试将 class Restaurant::Employee
更改为 class Restaurant::Helpers::Employee
并且在 PointsTest
中我更改为 employee1 = Restaurant::Helpers::Employee.create
然后我得到 这个错误:
未初始化常量 Restaurant::Helpers (NameError)
我做错了什么?我的意思是 Helpers
在子文件夹 helpers
!
您不能像 A::B::C::D
那样只 声明 链中的常量,因为要查找每个常量。
module Restaurant
module Helper # declare module Helper
class Employee
...
会起作用。而
# ⇓⇓⇓⇓⇓⇓ fail on try to const_get(:Helper)
class Restaurant::Helper::Employee
常量 Helper
查找失败,因为它未定义。希望对你有帮助。