Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. There i placed the both .rb files:

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

The spec_helper.rb is created normally in the spec folder. If i run:

rspec spec/lib/zombie_spec.rb

for the following code in 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:

cannot load such file -- zombie (LoadError)

zombie.rb looks exactly like this:

class Zombie
end
like image 499
CamelWriter Avatar asked Mar 18 '16 18:03

CamelWriter


3 Answers

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. RSpec does some magic with file paths so it might be looking in the wrong spot.

You might try either require_relative "./zombie" or move zombie.rb outside of your spec file and require "<path_to_zombie>/zombie".

like image 163
kclowes Avatar answered Nov 20 '22 05:11

kclowes


zombie.rb should be located in {project_home}/lib/ as pointed out by @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.

This question is months old but I am answering in case somebody stumbles upon this problem.

like image 38
John Doe Avatar answered Nov 20 '22 04:11

John Doe


In addition to kclowes answer you can add directories to your autoload paths within the rails configuration so that they're automatically required when Rails loads.

To do this simply add

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

to config/test.rb

like image 1
Anthony E Avatar answered Nov 20 '22 05:11

Anthony E