Stop Loading spec_helper from spec directory when running another set of tests - ruby

I have two spec folders. The first one is /spec and it what you typically would see. I have another called /live_integration_tests. When I run rspec -I live_integration_tests ./live_integration_tests I'm actually pulling in the spec/spec_helper.rb file as well. Is there a way to get rspec to ignore the spec_helper file in the spec_helper folder.
I've also tried rspec -O live_integration_tests/spec_helper.rb ./live_integration_tests
but that didn't do it either. Everything else in the help didn't look very promising.

I found a fix for this.
rspec --default-path ./live_integration_tests runs only the rspec spec_helper in the ./live_integration_test folder and not the default one in ./spec.

Related

Testing a Jekyll site with rspec and capybara, getting a bizarre race-case on rspec start

So check this out: it appears as though, upon running bundle exec rspec, there's a race between jekyll serve and puma/rspec's boot up. Sometimes I run the command, and my tests run fine. Other times, I get the error for each of my spec files: cannot load such file -- /path/to/project/sitename_jekyll/_layouts/spec/form_spec.rb which is interesting cause that's not where my spec files are located. They're in /path/to/project/sitename_jekyll/spec/form_spec.rb.
What's crazy is that I can literally just re-run the command over and over and over again and sometimes it'll go through and run the spec tests in the correct location, and sometimes it'll look for them in _layouts and error out. It probably runs correctly maybe once out of ever three or five attempts. All the other times I get the following errors:
Here's what my spec_helper.rb looks like: https://gist.github.com/johnhutch/2cddfafcde0485ff021501d5696c0c2d
And here's an example test file:
https://gist.github.com/johnhutch/a35d15c170f5fd9ca07998bf035d111d
My .rspec only contains two lines:
--color
--require spec_helper
And here's the output, both successful and unsuccesful, back to back:
https://gist.github.com/johnhutch/7927d609170ef5c70a595735502b128d
HEEELLLLLP!
This sounds like jekyll is changing the current directory while building the site, which since it is being run in a thread also affects the tests RSpec is trying to run (See https://bugs.ruby-lang.org/issues/9785 for why Dir.chdir is not threadsafe) - leading to attempts to load things from incorrect locations.
A potential solution to this would be to wait for the Jekyll site to be built before actually running your tests. A comment in your spec_helper seems to state that someone thought passing force_build: true would do this but from a quick perusal of the jekyll-rack code I don't think that's true and you actually need to wait for compiling? to return false (v 0.5) (complete? to return true in the current master branch) to ensure building has finished (as well as passing force_build). This could either be done in a loop sleeping and checking (simpler)
sleep 0.1 while <jekyll app>.compiling?
or (if using the master branch) via the mutex/conditional Rack::Jekyll exposes like in its test suite - https://github.com/adaoraul/rack-jekyll/blob/master/test/helper.rb#L49
Note: Also check my comment about your tests that aren't actually testing anything.
As per Thomas Walpole's super helpful responses this ended up working:
sleep 0.1 while Capybara.app.compiling?
inserted right after:
51 Capybara.app = Rack::Jekyll.new(force_build: true)
in my spec_helper.rb
Thanks again, Thomas!

File fixture getting deleted after running RSpec tests

I have tests written in RSpec that have been passing up until an upgrade to Ruby 2.5.1.
The test in question has several let statements in a before(:each) block. The file that keeps getting deleted is in there like so:
let(:fake_s3_file) { File.open(File.join(File.dirname(__FILE__), '../fixtures/Alexa.pdf')) }
Later in before(:each) stanza, I have this:
allow(s3_client).to receive(:get_object).and_return(fake_s3_file)
allow(Tempfile).to receive(:new).and_return(fake_s3_file)
It appears that in Ruby 2.5.1, my source files are getting deleted, either because of the File.open or the Tempfile reference. Has anyone run into this, and what is a good way around this? I haven't found any docs that say any changes have happened to Tempfile or to File.open. Should I copy my fixture file into a /tmp directory before every spec run? It seems wrong to do that, and slow.

outputting html reports using .rspec file

I am new to rspec testing.
I have a folder which contains _spec.rb files. When I run the specs using:
rspec --format html --out rspec_results.html "/Users/phukb/prpc-platform/prpc-platform/test/giza/coreui/ClientPerformance/RDL/spec/"
I see that the tests are run successfully and the results are posted in rspec_results.html.
However, when I create a .rspec file in my project with:
--format html --out rspec_results.html
and run the specs using
rspec "/Users/phukb/Desktop/prpc-platform/prpc-platform/test/giza/coreui/runtime_regression/featuresets/DynamicLayout/Repeating/MicroDC/Pagination/spec"
I see that the results_rspec.html is not getting created. Am I missing something here?
here is my folder structure
If you are running the rspec command from ../RDL folder,then the .rspec file should also be there.
That is, the path of the file should be ../RDL/.rspec, not ../RDL/spec/.rspec.

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

RSpec Autotest loops with failures, doesn't work when exceptions are added

I've been playing around with autotest trying to make it work all day.. but am having some problems...
I've been following https://github.com/rspec/rspec/wiki/autotest, I'm running with:
Ruby 1.9.3-p194
rspec 2.10.0
ZenTest 4.8.1
I also created a .rspec file.
So with this setup, I run autotest, and it works - my test runs, it passes, hooray!. When I stick a failure into my test e.g. false.should == true, then the test starts looping, over and over again.
what happens is that it's an integration test, and I'm writing to an sqlite db. If I run find . -mmin -1 then I'm able to see that my db folder has changed - so I figured this is the problem.
So I edit .autotest and add the following:
Autotest.add_hook :initialize do |autotest|
%w{db}.each { |exception| autotest.add_exception(exception) }
false
end
But now when I run autotest, it just says the following:
loading autotest/rspec2
and that's it, it won't do anything anymore. Previously the output was:
loading autotest/rspec2
/home/me/.rbenv/versions/1.9.3-p194/bin/ruby -rrubygems -S '/home/me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.10.1/exe/rspec' ``--tty '/home/me/Workspace/myproject/spec/integration/db/lead_spec.rb'
and then it'd run my test and show the result...
Anyone know what could be going on? it's very frustrating, and I feel like I've come to a road block....
Thanks for your help!
Autotest checks if defined exceptions match any part of the filename. Your spec has db in it's path so it is ignored by autotest.
If you want to ignore db folder, then do the following:
Autotest.add_hook :initialize do |a|
a.add_exception %r{^\./db}
end

Resources