21

I can typically test a regular Test::Unit method using the following commandline syntax for a method "delete_user_test":

ruby functional/user_controller_test.rb -n delete_user_test

Now when I'm using the shoulda plugin with Test::Unit I try to use the same technique as follows:

... 
context "Deleting a User" do
  should "remove user from user table" do
    ...
  end
end

Then I try to run the single test as follows:

ruby functional/user_controller_test.rb -n "test: Deleting a User should remove user from user table"

This doesn't work. Does anyone know how I can run a single context tests using shoulda and Test::Unit. I have a couple of different test in one test file and I want to only run the one using TDD without having to wait for all tests to run.

Joe Dean
  • 3,356
  • 5
  • 26
  • 31

3 Answers3

36

This works for me:

ruby functional/user_controller_test.rb -n "/Deleting a User/"

Just put some reasonably long string from your context name into the regular expression.

Milan Novota
  • 15,506
  • 7
  • 54
  • 62
  • Interestingly, using "/Deleting_a_User/" would work before using shoulda, but now you can, and actually have to, use "/Deleting a User/" – Magne Mar 28 '17 at 13:04
6

Using the full name of the test with a space at the end seems to work too:

ruby -Itest 
     functional/user_controller_test.rb 
     -n "test: Deleting a user should remove user from user table. "
Shadwell
  • 34,314
  • 14
  • 94
  • 99
3

Combining the two approaches has worked well for me; using both -I test and the regexp.

ruby -Itest functional/user_controller_teset.rb -n "/remove user from user table/"
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
Abel
  • 2,115
  • 3
  • 18
  • 24