user273072545345
user273072545345

Reputation: 1566

Trying to run a rspec test file and it can't find it

I am trying to learn RSpec, and thus starting with a simple calculator program.

Confused on how to have the rspec find the file because it keeps saying that it can't load it.

This is the process thus far:

1 Calculator class is written and saved as calculator.rb file. Create lib folder and move the calculator.rb file to it.

2 In the initial directory (i.e., outside of lib folder), I rspec --init which creates .rspec file & rspec/spec_helper.rb file.

3 Then I create write calculator_spec.rb file and put it in the rspec folder. On top of the calculator_spec.rb file, I have this require 'calculator.rb'

4 I run this command rspec calculator_spec.rb to which I get this 'load': cannot load such file

How do I fix this so the rspec command can find the file?

Thank you

Upvotes: 0

Views: 2911

Answers (2)

Jim Van Fleet
Jim Van Fleet

Reputation: 1118

This command would be in the first line of spec/spec_helper.rb:

require File.join(File.dirname(__FILE__), '../lib/calculator.rb')

Upvotes: 1

Midwire
Midwire

Reputation: 1100

Not sure which version of RSpec you are using but it should create a spec directory, not rspec in addition to the default .rspec file.

@jim-van-fleet's answer should work just fine. However, the best practice is to mirror your code directory under the spec directory. So it should look something like this:

rspec hierarchy

I would also add the following to the top of your calculator_spec.rb file

require 'spec_helper'

require File.expand_path('../../../lib/calculator', __FILE__)

Upvotes: 0

Related Questions