Problems running only one unit test on Rails 4

Asked

Viewed 201 times

4

When I run all the tests or just tests a file is working normally, but when I try to run just one test, it just doesn’t run.

I have this test

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "is ban" do
    user = build(:user, role: :ban)
    assert user.banned?
  end

end

However when I run on the console just this file, I have the answer expected in the tests

rake test/models/user_test.Rb

Run options: -Seed 34310

Running tests:

.

Finished tests in 0.041871s, 23.8831 tests/s, 23.8831 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

But when I want to run only one function in the same way as the Rails documentation explains. ( http://guides.rubyonrails.org/testing.html#running-tests )

rake test/models/user_test.Rb is_ban

Run options: -n is_ban --Seed 27215

Running tests:

Finished tests in 0.007734s, 0.0000 tests/s, 0.0000 assertions/s.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

I don’t have the test is_ban run, could someone explain to me how I can run just one specific test? I’ve tried to do it many ways and I haven’t succeeded.

I am using Rails 4.0.2, Ruby 2.0 and I have some test Gem installed as factory_girl, mocha and Shoulda.

  • 1

    Try rake test test/models/user_test.rb test_is_ban, as the documentation says. Note the test_ in front.

  • @William had not paid attention to that detail, but I think that was not so clear in the documentation. thank you very much.

1 answer

2


Quoting the documentation, in free translation:

Rails adds a method test which receives a test name and a block. It generates a normal test of the MiniTest::Unit, with the name of the method prefixed by test_. So,

test "the truth" do
  assert true
end

is the same as writing

def test_the_truth
  assert true
end

the macro test only allows a more readable name to the test. Either way you can still use normal method definitions.


Running the tests is as simple as invoking the file containing the test cases via the command test of rake.

$ rake test test/models/post_test.rb
.
 
Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
 
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

You can also run a particular test by running test and passing the name of the method.

$ rake test test/models/post_test.rb test_the_truth
.
 
Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s.
 
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

In short, use test_is_ban in place of is_ban at the command line.

rake test test/models/user_test.rb test_is_ban

Browser other questions tagged

You are not signed in. Login or sign up in order to post.