RSpec: kernel_require.rb: 45: in `require ': cannot load such file - bowling.rb (LoadError)

Following the example given here:

http://rspec.info

however, it fails with:

kernel_require.rb:45:in `require': cannot load such file -- bowling.rb (LoadError)

although I have a file bowling.rb.

Any suggestions?

UPDATE

List of projects:

ls -l
-rw-r--r--  1 snowcrash  snowcrash   77 10 Jul 19:43 bowling.rb
-rw-r--r--  1 snowcrash  snowcrash  205 10 Jul 19:49 bowling_spec.rb

$ rspec bowling_spec.rb 
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)

and code lists:

Spec:

# bowling_spec.rb
require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

Class file:

# bowling.rb
class Bowling
  def hit(pins)
  end

  def score
    0
  end
end
+1
source share
2 answers

The rspec homepage, unfortunately, does not talk about rspec initialization in your project.

Assuming you have a project folder called "bowling" in the bowling folder

rspec --init

This will create a specification directory and two files.

spec/spec_helper.rb
.rspec

The file .rspecallows you to define parameters such as color and format.

--color
--format documentation

Now in spec_helper.rbaddrequire "bowling"

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.

require "bowling"

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'
end

bowling_spec.rb `require 'spec_helper"

require "spec_helper"

class Bowling
  def hit(pins)
  end

  def score
    0
  end
end

, , , require "spec_helper". spec_helper.rb , .

rspec

.

+7

noob ruby ​​( java) , rspec. , .

:

rspec --init.

spec_helper.rb require_relative '../bowling'.

:

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
require_relative '../bowling'

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|

:

bowling_spec.rb:

# bowling_spec.rb    

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

bowling.rb:

# bowling.rb

class Bowling
  def hit(pins)
  end

  def score
    -1
  end
end

. , rspec --init, .

, - (rbenv), require_relative?

0

All Articles