I have the following 2 files in the same dir:
churn.rb:
subsystem_names = ['audit', 'fulfillment', 'persistence',
'ui', 'util', 'inventory']
start_date = month_before(Time.now)
puts header(start_date)
subsystem_names.each do | name |
puts subsystem_line(name, change_count_for(name))
end
churn_test.rb:
require "test/unit"
require "churn"
class ChurnTests < Test::Unit::TestCase
def test_month_before_is_28_days
assert_equal(Time.local(2005, 1, 1),
month_before(Time.local(2005, 1, 29)))
end
end
When I run churn_test.rb I get the following error:
/Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- churn (LoadError)
from /Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from churn-tests.rb:8:in `<main>'
When you require sth, Ruby searches the source in $LOAD_PATH ($:). For security concern, the current working directory is not included in $LOAD_PATH. You can either explicitly add the directory into $LOAD_PATH
$: << File.dirname(__FILE__)
require 'churn'
or use the Kernel#require_relative to require a module based on the same directory:
require_relative "churn"
Related
Im currently writing my test cases using ruby sinatra and im having a very weird or just im missing an important note/tip when writing a test cases using Test-Unit sinatra.
My problem is my my environments.rb is not being loaded in my test from my app.rb. but if i run it, it does get loaded.
here is my app.rb
require 'rubygems'
require 'sinatra'
require 'pg'
require './config/environments'
require './models/user'
module Registration
class HelloWorldApp < Sinatra::Base
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
get '/' do
#title = " Introduction Biatch"
erb :index
end
post '/register' do
DB[:users].insert(username: params[:username],password: params[:password])
redirect '/view'
end
get '/view' do
#users = User.all
erb :view
end
get '/view/:id' do
#user = User.find(id: params[:id])
erb :edit
end
post '/edit/:id' do
#user = User.find(id: params[:id])
#user.update(username: params[:username],password: params[:password])
redirect '/view'
end
get '/delete/:id' do
#delete_user = User.find(id: params[:id])
#delete_user.delete
redirect '/view'
end
end
end
my environments.rb is in the config folder.
and here is my sample test cases. (not yet finished)
require 'rubygems'
require 'test/unit'
require 'test/unit/assertions'
require '../app/app'
module Registration
class TestCrud < Test::Unit::TestCase
# include Registration::HelloWorldApp
def test_insert_user
end
def test_get_all_user
end
def test_delete_all_user
end
end
end
and this is the error it throws.
C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./config/environments (LoadError)
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Users/John/Documents/Sinatra-Intro/app/app.rb:4:in `<top (required)>'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from test_crud.rb:4:in `<main>'
C:\Users\John\Documents\Sinatra-Intro\test>
Im very confused what did i do wrong. thanks in advance.
Ive found the answer, just change the require './config/environments' to require_relative. and change the require '../app/app' into require_into.
I've added this to my spec_helper:
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
And my page is this:
class AboutPage < SitePrism::Page
end
My rspec is this:
require_relative 'spec_helper'
describe 'About Page' do
it "test" do
about = AboutPage.new
end
end
The error I keep getting is:
Failures:
1) About Page test
Failure/Error: about = AboutPage.new
NameError:
uninitialized constant AboutPage
# ./about_spec.rb:6:in `block (2 levels) in '
If AboutPage is not in the root path of controller, you must call it with a namespace like:
about = SomeNameSpace::AboutPage.new
It looks like I needed to add to my spec_helper.rb:
require_relative about_page.rb
All pages you are using in your spec files have to be required before the can be used.
If all of your site prism objects are in spec/page_objects, using rspec, you could add this line to rails_helper.rb
Dir[Rails.root.join('spec/page_objects/**/*.rb')].each { |f| require f }
This will automatically require about_page.rb and any other page objects, including subfolders.
I'm trying to implement some simple testing in rspec for a gem I'm writing. When I comment out describe BikeShare do down to end and run the file, the file loads in and runs successfully. I'm sure it's something tiny I'm missing.
My test file is really simple and looks like this:
require 'spec_helper'
describe BikeShare do
it "should run" do
# response = BikeShare.new
# response.should_be present
end
end
When run, I get the error uninitialized constant BikeShare (NameError) at line 3.
My bikeshare.rb file looks like this, fairly simple:
class BikeShare
def initialize
response = JSON.parse(open("http://bayareabikeshare.com/stations/json").read)
#response = response["stationBeanList"]
end
def get_last_station
#response.last["id"]
end
end
My Rakefile looks like this:
require 'rubygems'
require 'bundler'
Bundler.setup
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |spec|
# spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/*_spec.rb'
end
task :default => :spec
Your tests arent aware of BikeShare.
You need to require the file that defines your BikeShare class. I dont use rspec but I think that you normally set up your testing environment in spec_helper.rb.
When I do require './primes.rb' in irb I get this:
1.9.3-p392 :004 > require './primes.rb'
LoadError: cannot load such file -- ./primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):4
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Here is the primes.rb document:
# primes.rb
require 'debugger'
def prime?(num)
debugger
(1..num).each do |i|
if (num % i) == 0
return false
end
end
end
def primes(num_primes)
ps = []
num = 1
while ps.count < num_primes
primes << num if prime?(num)
end
end
if __FILE__ == $PROGRAM_NAME
puts primes(100)
end
Any suggestions of how to get this to work would be greatly appreciated!
When I do require relative it gives me this:
1.9.3-p392 :010 > require_relative 'primes.rb'
LoadError: cannot infer basepath
from (irb):10:in `require_relative'
from (irb):10
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
When I do the second solution below it gives me this:
1.9.3-p392 :013 > $LOAD_PATH << "."
=> ["/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/x86_64-darwin11.4.2", "."]
1.9.3-p392 :014 > require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):14
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :015 >
When I try it in pry:
[4] pry(main)> require_relative 'primes.rb'
LoadError: cannot infer basepath
from (pry):2:in `require_relative'
[5] pry(main)> require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
[6] pry(main)> .ls
Applications Movies git-completion.bash
Desktop Music rails_projects
Documents Pictures ruby
Downloads Public runwithfriends
Dropbox code shopify
Library dev sites
[7] pry(main)> require 'ruby/app_acad_mini_curriculum/debugging/primes.rb'
LoadError: cannot load such file -- ruby/app_acad_mini_curriculum/debugging/primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
Try require_relative
require_relative 'primes.rb'
EDIT:
Note that this will only work from within a script. If you are trying to require this script into an irb session then you will need to provide the full path to primes.rb. The reason is where irb's location is. For instance, try Dir.pwd inside irb and you will see where require_relative is attempting to search for primes.rb.
There are a couple things you could do:
# Just need to require the one file.
require_relative File.join('users', 'yourusername', 'prime_folder', 'prime.rb')
# Many files in the same folder
$LOAD_PATH << File.join('users', 'yourusername', 'prime_folder')
require 'prime.rb'
require 'another_file.rb'
Another option, one that I use, is Pry. It is like irb and is very easy to call from a script. It is a gem so:
gem install pry
At the end of your script, you could do:
if $0 == __FILE__
require 'pry'
binding.pry
end
You would then drop into an irb like REPL where you can test and debug your methods. I can't survive without it.
unlike ruby 1.8, you can't require a file that is in the same folder, because the current folder is not on the load path any longer.
To emulate the behavior of ruby 1.8, you could try
$LOAD_PATH << "."
require 'primes.rb'
However, the correct way to do in ruby 1.9, as #CharlesCaldwell pointed, is using relative_require.
Here is a good discussion of the best way to deal with this.
note that relative_require does not work in irb. You can check the motive on #CharlesCaldwell answer.
But looking in your task question, you should not use irb, you should use pry:
We're going to use two gems. One is called Pry, which is a replacement for irb. You'll have to gem install pry. It's not essential for debugging that you use Pry, but it will make life nicer.
Here is an example using relative require:
[fotanus#thing ~]$ cat primes.rb
# primes.rb
def prime?(num)
(1..num).each do |i|
if (num % i) == 0
return false
end
end
end
def primes(num_primes)
ps = []
num = 1
while ps.count < num_primes
primes << num if prime?(num)
end
end
if __FILE__ == $PROGRAM_NAME
puts primes(100)
end
[fotanus#thing ~]$ cat a.rb
require_relative 'primes.rb'
[fotanus#thing ~]$ ruby a.rb
I'm considering using IronWorker a project so that I can scale it easily (high traffic expected, with lots of background jobs).
In order to stay DRY, I'm trying to define workers using inheritance but I keep getting the following error:
/usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- base_worker.rb (LoadError)
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /task/child_worker.rb:3:in `<top (required)>'
from /task/runner.rb:344:in `require_relative'
from /task/runner.rb:344:in `<main>'
Here is the base worker class:
# app/workers/base_worker.rb
require 'net/http'
require 'uri'
require 'json'
class BaseWorker < IronWorker::Base
attr_accessor :params
# The run method is what IronWorker calls to run your worker
def run
data = custom_run(params)
common_post_process(data)
end
def custom_run(params)
#to be overwritten in the child class
end
def common_post_process(data)
# some common post processing => DRY
....
end
end
And here is a child class :
# app/workers/child_worker.rb
require 'net/http'
require 'uri'
require 'base_worker.rb'
class ChildWorker < BaseWorker
merge "base_worker.rb"
def custom_run(params)
#custom work
end
end
Any idea on how to fix this?
I'd recommend using our next generation gem, iron_worker_ng: https://github.com/iron-io/iron_worker_ruby_ng . The iron_worker gem is deprecated. And if you want to keep it similar style to what you have, your child_worker.rb might look like this:
require 'net/http'
require 'uri'
require_relative 'base_worker.rb'
class ChildWorker < BaseWorker
def custom_run(params)
#custom work
end
end
# NG gem doesn't run anything in particular, so to run your method:
cw = ChildWorker.new
cw.custom_run(params)
And in a child_worker.worker file:
runtime 'ruby'
file 'base_worker.rb'
exec 'child_worker.rb'
Then to upload it to IronWorker:
iron_worker upload child_worker
Then you can start queuing jobs for it:
worker = IronWorkerNG::Client.new
worker.tasks.create("child_worker", params)
If you use iron_worker_ng, it also possible to define a run method. This method will be called when the IronWorker runs. You have to specify the Class within the .worker file.
# child_worker.rb
class ChildWorker
def run
puts "doing the hard work"
end
end
And the child_worker.worker file:
# child_worker.worker
runtime 'ruby'
name 'ChildWorker'
exec 'child_worker.rb', 'ChildWorker'