E Pierre
E Pierre

Reputation: 426

Rspec error using ruby script: "cannot load such file -- ./components/tasks.rb" (ruby app not rails)

I am trying to build my Rspec testing test to test out a ruby app I am building. I know I should build first but test later. The code does work 100% I am just having issues getting Rspec to even look at my code.

The full code on github: https://github.com/EsdrasEtrenne/tictactoe

The only file that im running with rspec so far is ruby/spec/game_spec.rb

the game_spec.rb file looks like this:

require_relative "../tictactoe"
Rspec.describe Tasks do
  before(:each)do
    @game = Tictactoe::Game.new
  end

  it "has a working method called play" do
    expect{@game.play}.to output("WELCOME! To the unbeatable Tic-tac-toe").to_stdout
  end
end

It requires tictactoe as a relative:

require "./components/tasks.rb"
require "./components/board.rb"
require "./components/player.rb"
require "./components/player_types/computer.rb"
require "./components/player_types/human.rb"

module Tictactoe
  class Game
    attr_reader :board, :player, :opponent, :tasks

    def initialize
      @board = Board.new
      @tasks = Tasks.new
    end

    def play
      @tasks.greet_players
      @player, @opponent = @tasks.get_order
      current_player, current_opponent = @player, @opponent
      @tasks.print_board(@board)

      until @board.game_is_over || @board.tie
        @tasks.tell_turn(current_player)
        current_player.move(@board)
        @tasks.print_board(@board)
        if @board.game_is_over || @board.tie
          @tasks.game_over(@board)
          if @tasks.end_options
            game = Tictactoe::Game.new
            game.play
          end
        else
          current_player, current_opponent = current_opponent, current_player
        end
      end

    end

  end
end

game = Tictactoe::Game.new
game.play

Then I get this error when I run rspec game_spec.rb:

An error occurred while loading ./game_spec.rb.
Failure/Error: return gem_original_require(path)

LoadError:
  cannot load such file -- ./components/tasks.rb
# /Users/Esdras/Desktop/first_vagrant_box/coding_challenges/ruby/tictactoe.rb:1:in `<top (required)>'
# ./game_spec.rb:1:in `require_relative'
# ./game_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00028 seconds (files took 0.08732 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

The game works 100% regularly. I just am looking to make the first test pass and from there the rest should be really straight forward.

Upvotes: 0

Views: 9412

Answers (1)

max pleaner
max pleaner

Reputation: 26758

the require paths are resolved according to the dir you're in when you're executing the code. It's actually a bit more complicated than that, and there is this whole concept of "load path" which is configurable. See What are the paths that "require" looks up by default?

From this line An error occurred while loading ./game_spec.rb. I'm figuring you've run cd spec then rspec ./game_spec.rb or something like that. I think your code would work if you were in the root of the project directory and ran rspec spec/game_spec.rb.

The benefit of require_relative over require is that the paths can be resolved no matter where you call the script from. I think if you used require_relative in tictactoe.rb it would work.

Upvotes: 7

Related Questions