rspec Cannot load such file after working the first time - ruby

When running through the rspec course in codeschool I keep running into the same problem. I will set up as requested and after creating zombie_spec.rb and running rspec I get the proper output listed below:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
*
Pending:
A Zombie is named Ash
# Not yet implemented
# ./spec/lib/zombie_spec.rb:3
Finished in 0.00929 seconds
1 example, 0 failures, 1 pending
Randomized with seed 7259
As I continue on with the first video and create the class Zombie as mentioned I receive this error when running rspec again:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- zombie (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/Justin/rubyproject/spec/lib/zombie_spec.rb:2:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
After this I removed everything and uninstalled and reinstalled rspec. Retried it again and returned the same results.
Any clue what is going on?
Thank you in advance for your help!

require_relative will load the file from the same directory as the rspec file

I had pretty much the same issue, though mine never worked even once at first. Updating the zombie_spec.rb file to show the full path of my zombie.rb file seemed to get it working properly.
Eg:require "/home/me/ruby/spec/lib/zombie"

I use this and it worked
require_relative "zombie.rb"

Well if this still a problem I got into the same issue so I created a folder under spec/lib where I put all the codes and another spec/test where goes all the tests then it worked.
and I also added require_relative here is the code snippet
zombie_spec.rb
require 'spec_helper'
require_relative '../lib/zombie'
describe Zombie do
it "has a name called'Jack'" do
zb = Zombie.new
zb.name.should == "Jack"
end
it "has no brains" do
zb = Zombie.new
zb.should be_intelligent == false
end
end
zombie.rb
class Zombie
attr_accessor :name
def initialize
#name = "Jack"
end
def intelligent?
false
end
end

Related

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.
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.

Using Rspec should receive

I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end

Undefined method error when trying to run around hooks

I am trying to run given scenarios as many times per each account type. I found out in other thread that "around hooks" could be the best option to do it the DRY way.
Below is my code:
require 'watir-webdriver'
require 'page-object/page_factory'
require 'page_navigation'
require 'data_magic'
require_relative 'pages/login_page'
Before do
beforeCodeBlock
...
end
Around('#hooks_test') do|scenario, block|
DataMagic.load('default.yml')
account_type = {"listener" => DataMagic.yml["listener_data"],
"free" => DataMagic.yml["free_data"],
"premium" => DataMagic.yml["premium_data"]}
account_type .each {|key, value|
puts "Running scenario: #{scenario.name} as #{key} user"
visit_page(LoginPage)
on_page(LoginPage).sign_in(value["username"],value["password"]) #Login as
block.call
}
end
After do
afterCodeBlock
...
end
So when I run the feature file I get the following output and subsequent error:
Running scenario: Hooks test as listener user
undefined method `visit_page' for nil:NilClass (NoMethodError)
/myProject/features/support/hooks.rb:38:in `block (2 levels) in <top (required)>'
/myProject/features/support/hooks.rb:35:in `each'
/myProject/features/support/hooks.rb:35:in `Around'
Everything seems to be working as expected until the execution deals with the visit_page method that belongs to PageObject gem.
There are possibly two problems I see here.
First of all you are getting the error because you did not register the PageObject::PageFactory module with World. Add this line after the requires:
World(PageObject::PageFactory)
Second, you are using an Around block and calling the same Scenario multiple times. From what I am seeing, you are doing this so that it will exercise the same behavior with different logged in users. If the behavior is exactly the same, what are you accomplishing by running it three times?

RSpec shorter backtrace output for test failures

I'm using RSpec (latest Version, 2.12.2) to test a small Ruby class I'm working on. My problem is that when an RSpec test fails, the test output seems incredibly verbose, and shows a huge list of error messages, almost what seems to be a full backtrace. This means I have to scroll up to see the actual error message and the top of the trace.
I believe by default RSpec is supposed to do this but it doesn't seem to be doing it for me. For example, if I run rspec spec/my_spec.rb:132 (just run one test that's on L132), I get this output:
Failure/Error: #f.has_changed?("test").should be_true
expected: true value
got: false
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/handler.rb:33:in `handle_matcher'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/syntax.rb:53:in `should'
# /Users/JackFranklin/Dropbox/Sites/rubygems/filefixtures/spec/filefixtures_spec.rb:137:in `block (4 levels) in <top (required)>'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `instance_eval'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
Which, as you can see, is absolutely massive. I've not got any RSpec config files around that could be altering the configuration passed to RSpec.
Has anyone seen this before? I've tried Googling but searches have been fruitless.
Edit: I then set up config to make sure it was applying the default backtrace cleaning:
RSpec.configure do |config|
# RSpec automatically cleans stuff out of backtraces;
# sometimes this is annoying when trying to debug something e.g. a gem
config.backtrace_clean_patterns = [
/\/lib\d*\/ruby\//,
/bin\//,
/gems/,
/spec\/spec_helper\.rb/,
/lib\/rspec\/(core|expectations|matchers|mocks)/
]
end
But this doesn't make a difference. Looking at the output, it should be having most of it filtered out but it seems like the configuration isn't being applied?
Edit again:
In my config, I can even run cleaned_from_backtrace?(line) to see if a line should be cleaned. This returns true:
config.cleaned_from_backtrace?("/Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'")
But the actual output in the terminal remains the same!
Edit 3:
I am running RSpec with the command rspec, and nothing more. The project's .rspec file contains:
--color
--format progress
And there is no ~/.rspec file either that could be applying settings.
So after a bit more chatting with the RSpec folk on Github, I managed to hunt down the problem.
The problem was that by default, RSpec removes lines from the backtrace that match the Regex /gems/. I had my project within a folder with gems in the name: ~/Dropbox/rubygems/myproject, so every line of the backtrace was being removed, and when that happens, RSpec sensibly shows you the entire backtrace.
That explains the behaviour I was seeing.
It's always something silly like that. I hope if anyone else does this, perhaps this answer will save them some time.
Looks like you're running RSpec with -b, for a full backtrace. Normally, RSpec won't show its own internal backtrace like that, even when your test fails badly (i.e. throws an exception rather than just failing an assertion). If you're not explicitly running it with -b or --backtrace, check and make sure that you don't have that set in a .rspec config file, or that your IDE or whatever isn't passing it.
As Jim said, there's a -b or --backtrace option that will enable the full backtrace. Bear in mind that it's not just the .rspec file that could be triggering it; there's also ~/.rspec (for a developer's preferred options).
The other thing to bear in mind is that a formatter can spit out whatever portion of the backtrace that it wants. The formatters expose a simple way for any 3rd-party formatter to respect the backtrace-filtering configuration, but it you're using a 3rd-party formatter there is no guarantee it is using that properly. Are you using one of the built-in rspec formatters?
Finally, if it's not one of those things, I'm out of ideas. I'd have to have an example to play with to answer your question. Can you come up with a reproducible example (in a gist, hopefully)? I try to hang out in the rspec channel of irc.freenode.net regularly, so maybe you can catch me there and we can do some troubleshooting that way.
From version 3.4.0 / 2015-11-11 it is possible to filter backtrace by option:
RSpec.configure do |config|
config.filter_rails_from_backtrace!
end

How to set the path to the classes being tested with Cucumber?

I've tried following two written guides and one youtube video (http://www.youtube.com/watch?v=8ZmawOCDeyo) and they either skip over an important step (or it just works for them) but not for me.
I have created the following folder structure:
RubyBots\
features\
step_definitions\
creating_a_vehicle_for_an_ai_bot_steps.rb
support\
env.rb
creating_a_vehicle_for_an_ai_bot.feature
vehicle.rb
vehicle_factory.rb
Contents of creating_a_vehicle_for_an_ai_bot_steps.rb:
Given /^I have vehicle factory$/ do
#vehicle_factory = VehicleFactory.new
end
When /^I use it to build a basic vehicle$/ do
#new_vehicle = #vehicle_factory.build
end
Then /^I should have receive a basic vehicle$/ do
#test #new_vechile
end
Contents of vehicle_factory.rb:
class VehicleFactory
def initialize(name)
p name
end
end
Contents of env.rb file:
require 'vehicle_factory'
require 'vehicle'
I execute Cucumber (with no parameters) from inside a CMD window inside the RubyBots directory and I receive the following error:
no such file to load -- vehicle_factory (LoadError)
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'
D:/Code/RubyBots/features/support/env.rb:1:in `<top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/rb_support/rb_language.rb:143:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/rb_support/rb_language.rb:143:in `load_code_file'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:176:in `load_file'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:78:in `block in load_files!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:77:in `each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:77:in `load_files!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime.rb:137:in `load_step_definitions'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime.rb:39:in `run!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/cli/main.rb:43:in `execute!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/cli/main.rb:20:in `execute'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/bin/cucumber:14:in `<top (required)>'
C:/Ruby192/bin/cucumber:19:in `load'
C:/Ruby192/bin/cucumber:19:in `<main>'
If I delete the env.rb file from the support directory I get the follow error when running Cucumber:
Scenario: Using a vehicle factory to build a basic vehicle # Features\creating_a_vehicle_for_an_ai_bot.feature:6
Given I have vehicle factory # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:1
uninitialized constant Object::VehicleFactory (NameError)
./Features/creating_a_vehicle_for_an_ai_bot_steps.rb:2:in `/^I have vehicle factory$/'
Features\creating_a_vehicle_for_an_ai_bot.feature:7:in `Given I have vehicle factory'
When I use it to build a basic vehicle # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:5
Then I should have receive a basic vehicle # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:9
Failing Scenarios:
cucumber Features\creating_a_vehicle_for_an_ai_bot.feature:6 # Scenario: Using a vehicle factory to build a basic vehicle
I've tried everything I can think of and my google-fu is failing me - I hope it's something non-trivial so I'm not embarrassed, but easy to fix, so that I can move on! :)
Thanks for the help!
Your class is called VehicleFactory but you're trying to call vehicle_factory.new in creating_a_vehicle_for_an_ai_bot_steps.rb.
Also, I believe Cucumber expects a particular directory structure:
RubyBots/
features/
step_definitions/
creating_a_vehicle_for_an_ai_bot_steps.rb
support/
env.rb
creating_a_vehicle_for_an_ai_bot.feature
You'll need to require your own files from env.rb appropriately. Ruby doesn't work out any paths for you. One way of auto-requiring files based on symbol name (the way Rails does it) is described in my blog post (it relies on ActiveSupport 2.3 but should be similar for the current version).

Resources