简体   繁体   English

Rspec'无法加载此类文件'

[英]Rspec 'cannot load such file'

I am doing the Testing with Rspec course of Code School and have installed ruby 2.2.1, rails 4.2.5.1 and rspec 3.4.4 to my Ubuntu system. 我正在使用Code School的Rspec课程进行测试,并已将ruby 2.2.1,rails 4.2.5.1和rspec 3.4.4安装到我的Ubuntu系统中。 As the first video describes I typed 正如我输入的第一个视频描述

rspec --init

in my project folder, which created a spec folder, in which i made a new directory called lib. 在我的项目文件夹中,它创建了一个spec文件夹,在其中我创建了一个名为lib的新目录。 There i placed the both .rb files: 在那里我放置了两个.rb文件:

touch spec/lib/zombie_spec.rb
touch spec/lib/zombie.rb

The spec_helper.rb is created normally in the spec folder. spec_helper.rb通常在spec文件夹中创建。 If i run: 如果我跑:

rspec spec/lib/zombie_spec.rb

for the following code in zombie_spec.rb: 对于zombie_spec.rb中的以下代码:

require "spec_helper"
describe "Zombie" do
    it "is named Ash"
end

everything runs as expected and shown in the video. 一切都按预期运行并显示在视频中。 But if i take the next step and add 但如果我采取下一步并添加

require "zombie"

to the zombie_spec.rb file after the first require, I get the error: 在第一次需要之后到zombie_spec.rb文件,我收到错误:

cannot load such file -- zombie (LoadError)

zombie.rb looks exactly like this: zombie.rb看起来完全像这样:

class Zombie
end

Typically you don't want your zombie.rb inside your spec folder since it's not a test file, but I'm not familiar with the Code School tutorials. 通常,您不希望zombie.rb位于spec文件夹中,因为它不是测试文件,但我不熟悉Code School教程。 RSpec does some magic with file paths so it might be looking in the wrong spot. RSpec对文件路径做了一些魔术,所以它可能在错误的位置查找。

You might try either require_relative "./zombie" or move zombie.rb outside of your spec file and require "<path_to_zombie>/zombie" . 您可以尝试require_relative "./zombie"或将zombie.rb移动到spec文件之外并require "<path_to_zombie>/zombie"

zombie.rb should be located in {project_home}/lib/ as pointed out by @kclowes. zombie.rb应该位于{project_home}/lib/如@kclowes所指出的那样。

In your case since it lives in the same folder (not recommended), you should use the format: require './zombie' and not require 'zombie' because the latter is used for standard and/or third party libraries. 在你的情况下,因为它存在于同一个文件夹中(不推荐),你应该使用以下格式: require './zombie' require 'zombie'而不require 'zombie'因为后者用于标准和/或第三方库。

This question is months old but I am answering in case somebody stumbles upon this problem. 这个问题已经有几个月了,但我正在回答,以防万一有人偶然发现这个问题。

In addition to kclowes answer you can add directories to your autoload paths within the rails configuration so that they're automatically require d when Rails loads. 除了kclowes之外,您还可以在rails配置中的自动加载路径中添加目录,以便在加载Rails时自动require d。

To do this simply add 要做到这一点,只需添加

config.autoload_paths << "#{config.root}/spec/lib"

to config/test.rb to config/test.rb

If you only want to require a file in exactly one other file, this is another possibility: 如果您只想在一个其他文件中要求一个文件,这是另一种可能性:

require "#{::Rails.root}/spec/support/models/shared_examples/<my_file>"

(Rails 6.0.x) (Rails 6.0.x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM