How to Run a Single Rspec Test

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 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 do I run only specific tests in Rspec?

You can tag examples with :focus hash attribute. For example,

# spec/foo_spec.rb
RSpec.describe Foo do
it 'is never executed' do
raise "never reached"
end

it 'runs this spec', focus: true do
expect(1).to eq(1)
end
end
rspec --tag focus spec/foo_spec.rb

More info on GitHub. (anyone with a better link, please advise)

(update)

RSpec is now superbly documented on relishapp.com. See the --tag option section for details.

As of v2.6 this kind of tag can be expressed even more simply by including the configuration option treat_symbols_as_metadata_keys_with_true_values, which allows you to do:

describe "Awesome feature", :awesome do

where :awesome is treated as if it were :awesome => true.

Also, see this answer for how to configure RSpec to automatically run 'focused' tests. This works especially well with Guard.

Rails Rspec - Run only single test

Try this to run a single test:

rspec ./spec/parent/child/spec.rb:line_number_of_your_test

line_number_of_your_test can be any number spanning the test if your it block is a multi-line block.

Update: Removed 'rand:seed' part.

RSpec Is there a way to run all tests with one command?

Try this in your terminal window:

bundle exec rspec spec

Is it possible to run one RSpec test before the others?

Yes it is feasible, see this post: https://thoughtbot.com/blog/testing-your-factories-first. The approach is to create a factories_spec and then add it as a pre-requisite to the rake file.

(1) Create a factories spec file:

 # spec/factories_spec.rb    
FactoryBot.factories.map(&:name).each do |factory_name|
describe "The #{factory_name} factory" do
it 'is valid' do
expect(build(factory_name)).to be_valid
end
end
end

(2) To add a pre-requisite to your rake spec:

# Rakefile    
if defined?(RSpec)
desc 'Run factory specs.'
RSpec::Core::RakeTask.new(:factory_specs) do |t|
t.pattern = './spec/factories_spec.rb'
end
end

task spec: :factory_specs

(3) Run your test with

rake spec

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

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.



Related Topics



Leave a reply



Submit