How do I require a ruby file from child directory - ruby

I have the following structure
tests
specific tests
first_test.rb
more tests
second_test.rb
test_helper.rb
Now I am trying to require test_helper in both first_test and second_test. The following works
require '../test_helper.rb'
and from command line I should be under specific tests and execute
ruby first_test.rb
How can I get more flexibility ie. I want to be in any directory and execute these tests. Currently I am getting 'no such file'. This is not a rails app but in rails I could simply do require 'test_helper.rb'
Bonus points for making this work with rake.

Put your test_helper and other helpers in a separate directory like
/home/XXX/projects/test_project/lib/
or wherever you like and use base directory like
Dir["base/path/to/your/helper/*.rb"].each {|file| require file }
or from my above example
Dir["/home/XXX/projects/test_project/lib/*.rb"].each {|file| require file }
Thats it.
Now add this line to the beginning of every test you want to run separately from any directory.And forget about the directory issue.

Related

Unable to include file in the spec file

I am trying to write an RSpec test for a ruby project(Sketchup plugin) but I am facing an issue with the require.
Below is how my folder is structured
In my smoke_tests_spec.rb
require "main_folder/subfolder1/file_to_test.rb"
When I run the rspec using rspec-core/rspec from the root directory I get the following error
Failure/Error: require "main_folder/subfolder1/file_to_test.rb"
LoadError:
cannot load such file -- main_folder/subfolder1/file_to_test.rb
You need to change the LOAD_PATH or use require_relative
Here you have more info.

Ruby: Require Fails To Import - Need To Set Root Directory

Forgive my inexperience with Ruby, but I am unable to run a script within a third-party project with the following structure:
˅ alpha
˅ lib
˅ bravo
golf.rb
˅ charlie
˃ delta
˅ echo
foxtrot.rb
require "charlie/delta/echo/__init"
__init.rb
require "bravo/golf"
What should my command-line be to run the script, 'foxtrot.rb', as the following generates an error:
ruby "c:\arby\lib\bravo\charlie\delta\echo\foxtrot.rb"
"'require': cannot load such file -- charlie/delta/echo/__init (LoadError)"
If this is the code inside of __init.rb, it won't work.
require "charlie/delta/echo/__init"
__init.rb
require "bravo/golf"
require tells ruby to load the code inside a ruby file. In order for it to work, the files need to be organized correctly. You can also use require_relative but they still need a relative path from the file calling them. See What is the difference between require_relative and require in Ruby?

Rspec: requiring spec/workers

I've got a bunch of Sidekiq workers in app/workers, and some matching specs in spec/workers. How can I add both to my rspec runs? As in stands none are included when I hit up rspec on the Terminal.
I see a few alternatives for your situation:
Rspec loading:
Make sure your test files in spec/worker follow the pattern setup for Rspec (e.g. *.rb or *_spec.rb).
Require the worker files in the spec_helper.rb.
Manual loading: Require the spec/worker and app/worker files in your spec_helper.rb with a Ruby-defined glob.
Sidekiq helper gem: Add the rspec-sidekiq gem to your project, as explained at Sidekiq's.
To add a bulk of files to the load path, you can either:
Add path strings to the $: global variable, early in your boot process (e.g. at the beginning of spec_helper.rb):
$:.unshift(File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))
Use a glob to load the files without modifying the load path:
Dir[File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))].each do |file|
require file
end

Can RubyMine IDE recognize rspec tests if filename does not have _spec suffix?

Let me preface this by saying I am a newbie to Ruby.
I am doing an extremely basic tutorial on Ruby in RubyMine, and as part of the tutorial, we create a class in a .rb Ruby file called thing.rb. In addition to the class, the tutorial has us put an Rspec test in the same .rb file. The .rb file looks like the following:
require 'rspec'
class Thing
def value
5
end
end
describe Thing do
it "should have a value of 6" do
Thing.new.value.should eq(7)
end
end
If I right-click the project folder and "Run all specs in the project", I get errors and messages including "0 files found" and "Empty test suite." If I just run the rb script, I get no errors...the script runs fine.
The STRANGE thing is that if I add a second file to the project called thing_spec.rb and put in it only the test from above:
require 'rspec'
require_relative 'thing'
describe Thing do
it "should have a value of 6" do
Thing.new.value.should eq(7)
end
end
And then right-click the project and select "Run all specs," the tests run fine. Notice I said the tests (plural)...RubyMine will run the test in thing.rb AND the test in thing_spec.rb.
Why won't it recognize the test in thing.rb without my having to add a second file called thing_spec.rb?? This makes no sense to me. Is there anyway I could have it run the rspec tests from within thing.rb without having to create a second file?
In case it is important: I work on a Mac, have RubyMine version 4.5, using ruby interpreter 1.9.3, rspec 2.12.0.
If you go in Run > Edit Configurations...
And select All Specs in <Your Project> you'll see Filename Mask: **/*_spec.rb
This means when you tell RubyMine to run "All Specs", it only runs files which match the regex.
Now when you add second file and you do require_relative to load the other one, so both the tests run.

Ruby gem testing before deployment

I'm creating a gem which has
several scripts in the bin directory
the utility classes in the lib directory
and several tests in the test directory
supertool
bin
toolA
toolB
lib
supertool
supertool.rb
helper.rb
test
tc_main.rb
tc_etc.rb
Now, to run the tests before I even install the gem, I have the following snippet at the top of my tests:
base = File.basename(Dir.pwd)
if base == 'test' || base =~ /supertool/
Dir.chdir('..') if base == 'test'
$LOAD_PATH.unshift(Dir.pwd + '/lib')
Dir.chdir('test') if base =~ /supertool/
end
This seems tedious though, especially if I have to put these in the scripts in the bin directory too. Is there a better way of setting up the environment so we can test gems before they are installed? I'm sure it's something simple that I just can't find. A simple link to the right place would help a lot :)
I'm not sure what you're trying to achieve with that script. It doesn't seem to have anything to do with gems...
Is it so that you can run ruby tc_main.rb from within the test directory (or ruby test/tc_main.rb from the base dir), and have it set the load path appropriately? If so, here's a much nicer way:
In your test directory, create a test_helper.rb file. In that file, put this
$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
And in all your test files, set the first line to
require 'test_helper'
If you have subdirectories inside your test dir, then files in those subdirs can just do
require '../test_helper'
Take a look at hoe gem, it is a helper for other gems.

Resources