好的,所以我有这样的文件结构(包括相关部分)
| app -
| lib -
| search.rb
|
| models -
|
| api -
|
| people.rb
我希望图表不是太糟糕。
我正在尝试创建一个搜索库,该搜索库将使用people.name
查询搜索LIKE
。
# /app/models/api/people.rb
class Api::People < ActiveRecord::Base
end
# /app/lib/search.rb
class Search
def Search.execute(param)
@people = Api::People.find(:all, :conditions => ["name LIKE ?", param])
end
private
end
现在,你可以说,我是初学者。当我尝试使用此规范运行rake spec
命令时
# /spec/lib/search_spec.rb
require_relative '../../app/lib/search.rb'
describe Search do
it "is instantiable" do
@search = Search.new()
expect(@search).not_to eql(nil)
end
it ".execute does not throw error" do
@search = Search.execute("string")
end
end
错误报告:rake spec
Failures:
1) Search .execute does not throw error
Failure/Error: @people = Api::People.find(:all, :conditions => ["name LIKE ?", param])
NameError:
uninitialized constant Search::Api
# ./app/lib/search.rb:3:in `execute'
# ./spec/lib/search_spec.rb:10:in `block (2 levels) in <top (required)>'
Finished in 0.00118 seconds (files took 0.08766 seconds to load)
2 examples, 1 failure
我对这个Api::Search
常数感到很困惑。我尝试将people.rb
文件移出models/api
文件夹,但后来出现uninitialized constant Search::People
错误。如果你能提供帮助,那将非常感激。
答案 0 :(得分:0)
Rails正在将app/
中的文件夹添加到自动加载中,但您可能需要重新启动服务器(以防万一)可能会执行spring stop
。
对于规范,通常有一个require 'rails_helper'
(如果你使用的是RSpec v2,则为spec_helper
),它会使用自动加载路径添加Rails逻辑。
您的例外情况表明您的app/models/api/people
未加载。
另一个选择是添加:
# /spec/lib/search_spec.rb
require_relative '../../app/lib/search.rb'
require_relative '../../app/models/api/people.rb'