'cannot load such file -- mongoid' with rspec with ruby 2 - ruby

Getting this error
cannot load such file -- mongoid
when trying to run RSpec tests.
I have mongo running in another terminal (mongod)
with ruby 2.0
I've trying including gem 'moped' and bundled but got the same error.
Trying to find out how to fix this error and run my tests.
This is for an open-source project that I've forked and am trying to update.
The test starts with:
require 'spec_helper'
begin
require 'will_paginate/mongoid' # <-- this is the issue
rescue LoadError => error
warn "Error running Sequel specs: #{error.message}"
mongoid_loaded = false
else
Mongoid.database = Mongo::Connection.new.db('will_paginate_test')
class MongoidModel
include Mongoid::Document
end
mongoid_loaded = true
end
describe...
I commented out require 'will_paginate/mongoid' but I then got uninitialized constant Mongoid (NameError)

Related

Error when trying to run rspec Failure/Error: require 'test' LoadError: cannot load such file -- test

I am trying to run RSpec for Ruby on Rails. I am running Rails 5.0 I have installed the gem, have established a spec folder with some tests.
I run following command on console
$ rspec --init
create spec/spec_helper.rb
create .rspec
I create file called 'test.rb'.
class Test
end
I create file called 'zombie_spec.rb'.
require 'spec_helper'
require 'test'
describe Test do
it " Name Is Bapu." do
# test = test.new
# test.name.should == 'bapu'
end
end
Then after I run this command.
rspec spec/lib/zombie_spec.rb
It shows error ⬇
An error occurred while loading ./spec/lib/zombie_spec.rb.
Failure/Error: require 'test'
LoadError:
cannot load such file -- test
# ./spec/lib/zombie_spec.rb:2:in `require'
# ./spec/lib/zombie_spec.rb:2:in `<top (required)>'
No examples found.
Finished in 0.00026 seconds (files took 0.40148 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
How do I resolve this so that I can start running tests?
Your file paths are off, you either need to have zombie_spec.rb in the spec/ folder instead of spec/lib/ or you need to move test.rb into the lib/ folder instead of being in the / project root folder.

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.

require ruby library in serverspec

I am a newbie in ruby and trying to get my hands dirty in chef. I have written a wrapper cookbook on postgresql community cookbook and wish to test it using test kitchen. Following is the spec.rb file I have written:
require 'serverspec'
require 'pg'
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
c.os = backend(Serverspec::Commands::Base).check_os
end
end
describe "Postgresql server" do
it "should connect to database" do
conn = PG::Connection.open(:dbname => "db",:user => "user1",:password => "password")
conn.status == "CONNECTION_OK"
end
end
Through this test I wish to check if the user and database have been created properly.
However this test is unable to resolve the dependency of "pg". Where do I mention this dependency in serverspec?
I have used kitchen verify [node name] to run the test.
Create the Ruby code necessary to install the gem prior to requiring it in your spec_helper.rb file (or on the top of the spec file if it makes more sense):
begin
Gem::Specification.find_by_name('pg')
rescue Gem::LoadError
require 'rubygems/dependency_installer'
Gem::DependencyInstaller.new(Gem::DependencyInstaller::DEFAULT_OPTIONS).install('pg')
end
require 'pg'

Trying to create a proper ruby application (gem) for my Boulder codeschool class: lib classes not communicating correctly ('Uninitialized constant')

You can see everything in the file structure at github.com/ddd1600/simple_angel
I'm getting alot of errors while trying to create a ruby application (soon to be a gem) "the correct way", viz. by thoroughly dividing the logic into classes and "loader files" and all of that. Point is, I know how to do this code the simpler way, without obeying OO principles, but I want to do it "correctly".
So, first of all, the file structure is as follows---
root folder = ~/Develop/simple_angel
inside /simple_angel
- /lib
- Gemfile
- Rakefile
- simple_angel.gemspec
inside /lib
- simple_angel.rb
- /simple_angel
inside /lib/simple_angel
- company.rb
- search.rb
- version.rb
But, here are some basics.
Here is what I'm calling to run this program from the terminal (PATH when running is ~/Develop/simple_angel)
ruby -Ilib lib/simple_angel/search.rb
Here is search.rb
#these 'requires' are supposed to be loaded in lib/simple_angel.rb, so here I show
#them commented out
#
#require 'rubygems'
#require 'httparty'
#require 'json'
#require 'company'
module SimpleAngel
class Search
SEARCH_URL = "http://api.angel.co/1/startups"
def search(user_input)
response = HTTParty.get("#{SEARCH_URL}/#{user_input}")
parsed_response = JSON.parse(response.body)
Company.new(parsed_response)
end
end
s = SimpleAngel::Search.new
s = Search.new
x = s.search(6702)
p x
end
Here is the "loader" file, lib/simple_angel.rb (PS: what is a more formal title for this sort of file?)
require 'httparty'
require 'json'
require 'simple_angel/search'
require 'simple_angel/version'
require 'simple_angel/company'
module SimpleAngel
end
Lastly, when I (again), run "ruby -Ilib lib/simple_angel/search.rb" (with all of search.rb's 'requires' commented out (^&^), this is my error message:
[ddouglas#coders:~/Develop/simple_angel on master]
% ruby -Ilib lib/simple_angel/search.rb
lib/simple_angel/search.rb:15:in `search': uninitialized constant SimpleAngel::Search::HTTParty (NameError)
from lib/simple_angel/search.rb:24:in `<module:SimpleAngel>'
from lib/simple_angel/search.rb:8:in `<main>'
^&^ - now that we're all up to speed here, I might as well include the error that happened when I left search.rb's "requires" in place
% ruby -Ilib lib/simple_angel/search.rb ✹
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- company (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from lib/simple_angel/search.rb:6:in `<main>'
I can't say I understand the point the class is trying to make regarding splitting up your files.
The first error (uninitialized constant SimpleAngel::Search::HTTParty) is because from within SimpleAngel::Search, you call HTTParty. Try changing that to ::HTTParty to specify the root namespace.
The big picture issue, looking back on this issue, was that I probably just needed to activate my /lib folder within rails via this addition to config/application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
from there, code can be directly loaded via the file_name.rb => FileName class system from anywhere within rails

Running RSpec Files From ruby code

I'm trying to run RSpec tests straight from ruby code. More specifically, I'm running some mysql scripts, loading the rails test environment and then I want to run my rspec tests (which is what I'm having trouble with)... I'm trying to do this with a rake task. Here is my code so far:
require "spec/autorun"
require"spec"
require "spec/rake/spectask"
RAILS_ENV = 'test'
namespace :run_all_tests do
desc "Run all of your tests"
puts "Reseting test database..."
system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\BrianSite_test_CreateScript.sql"
puts "Filling database tables with test data..."
system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\Fill_Test_Tables.sql"
puts "Starting rails test environment..."
task :run => :environment do
puts "RAILS_ENV is #{RAILS_ENV}"
# Run rspec test files here...
require "spec/models/blog_spec.rb"
end
end
I thought the require "spec/models/blog_spec.rb" would do it, but the tests aren't running. Anyone know where I'm going wrong?
UPDATE: I've added the require "spec/autorun" command at the top of the file and now I am running into this error when I do a rake run_all_tests:run :
C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options.rb:283:in fi
les_to_load': File or directory not found: run_all_tests:run (RuntimeError)
from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options.
rb:275:ineach'
from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options.
rb:275:in files_to_load'
from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options.
rb:133:inrun_examples'
from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:61:in
run'
from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:45:in
autorun'
from C:/Ruby/bin/rake:19
It's hitting this error when it gets to the require "spec/models/blog_spec.rb" line. This file does exist because when I try and change the require statement, I just get a file not found error. It seems like rspec is trying to now run the tests, but is running into problems... any thoughts?
Thanks for any help.
Try adding require "spec/autorun to the top of your file.
You don't need to do it that way though, because there are built in Rake tasks (that's what the spec/rake/spectask is including) to do what you're doing: http://rspec.info/documentation/tools/rake.html

Resources