Hom*_*ith 32 rspec ruby-on-rails
我有一个测试,试图测试位于lib文件夹中的类.
现在我在parser_spec.rb中执行此操作
require 'spec_helper'
require 'parser' --> Because my class is /lib/parser.rb
describe "parser" do
it "needs a url to initialize" do
expect { Parser.new }.to raise_error(ArgumentError)
end
end
Run Code Online (Sandbox Code Playgroud)
包含所有lib文件的正确方法是什么,以便它们在rspec测试的范围内?
Try this
require_relative "../../lib/parser.rb"
or
require 'lib/parser.rb'
Run Code Online (Sandbox Code Playgroud)
rspec运行时会自动加载'spec / spec_helper.rb',并且还会自动将'lib'文件夹添加到它的LOAD_PATH中,以便可以正确地看到和要求您在'lib / parser.rb'中的要求。
只需将“lib”文件夹放入 autoload_path 即可。例如,在 application.rb 中
config.autoload_paths += "#{Rails.root}/lib/"
Run Code Online (Sandbox Code Playgroud)
然后就可以正常进行了
require 'spec_helper'
describe Parser do
...
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16585 次 |
| 最近记录: |