Execute Rspec from Ruby

How do you run a single test/spec file in RSpec?

Or you can skip rake and use the 'rspec' command:

bundle exec rspec path/to/spec/file.rb

In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.

If you're using an older version of rspec it is:

bundle exec spec path/to/spec/file.rb

How to run a single RSpec test?

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

RSpec.configure do |config|
config.filter_run_when_matching :focus
end

And then add a focus tag to the it, context or describe to run only that block:

it 'runs a test', :focus do
...test code
end

RSpec documentation:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

How to write Rspec test for running file from command line?


Don't Use the RSpec Output Matcher

RSpec has a built-in output matcher than can test both where output goes, as well as its contents. However, it's testing where your Ruby output goes, not whether some external application is using standard input or standard error. You're going to have to make some different assumptions about your code.

You can avoid driving yourself nuts by comparing strings rather than testing the underlying shell or your output streams. For example, consider:

RSpec.describe "parse utility output" do
it "prints the right string on standard output" do
expect(`echo hello world`).to start_with("hello world")
end

it "shows nothing on standard output when it prints to stderr" do
expect(`echo foo >&2 > /dev/null`).to be_empty
end
end

Just replace the echo statements with the correct invocation of parse for your system, perhaps by setting PATH directly in your shell, using a utility like direnv, or by modifying ENV["PATH"] in your spec or spec_helper.

As a rule of thumb, RSpec isn't really meant for testing command-line applications. If you want to do that, consider using the Aruba framework to exercise your command-line applications. It's best to use RSpec to test the results of methods or the output of commands, rather than trying to test basic functionality. Of course, your mileage may vary.

How do I run an RSpec test on a specific line?

You'll want to use the format rspec path/to/spec.rb:line_no.

(i.e.) rspec spec/models/orders_spec.rb:16

Here's a link to RelishApp (the best location for the RSpec documentation) if you'd like some more reading.

How to run multiple specific RSpec tests?

You can use the following syntax to run your specs for multiple files:

rspec path/to/first/file path/to/second/file


Related Topics



Leave a reply



Submit