Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test only one it or describe with Rspec

On TestUnit you can launch one test in file with -n option

for example

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "the truth" do
    assert true
  end

  test "the truth 2" do
    assert true
  end

end

You can execute only the test the truth

ruby -Itest test/unit/user_test.rb -n test_the_truth

The ouput

1 tests, 1 assertions, 0 failures, 0 errors, 0 skip

How can that with rspec ?

The command seem not work

rspec spec/models/user_spec.rb -e "User the truth"
like image 615
Joel AZEMAR Avatar asked Dec 21 '22 12:12

Joel AZEMAR


1 Answers

You didn't include the source of your spec, so it's hard to say where the problem is, but in general you can use the -e option to run a single example. Given this spec:

# spec/models/user_spec.rb
require 'spec_helper'
describe User do

  it "is true" do
    true.should be_true
  end

  describe "validation" do
    it "is also true" do
      true.should be_true
    end
  end

end

This command line:

rspec spec/models/user_spec.rb -e "User is true"

Will produce this output:

Run filtered including {:full_description=>/(?-mix:User\ is\ true)/}
.

Finished in 0.2088 seconds
1 example, 0 failures

And if you wanted to invoke the other example, the one nested inside the validation group, you'd use this:

rspec spec/models/user_spec.rb -e "User validation is also true"

Or to run all the examples in the validation group:

rspec spec/models/user_spec.rb -e "User validation"
like image 98
Rob Davis Avatar answered Dec 28 '22 07:12

Rob Davis