Running a Single Test with Spaces in Minitest

In Rails, if you wish to run a particular take a look at with out the others within the file, you’ll be able to move the strategy’s title to the --name flag.

def test_controller_name
  # assert ...
finish

>  ruby -I take a look at take a look at/controller/renderer_test.rb --title test_controller_name
Enter fullscreen mode

Exit fullscreen mode

However, Rails additionally offers a take a look at technique that takes a take a look at title and a block.

take a look at "creating from a controller" do
  # assert ...
finish
Enter fullscreen mode

Exit fullscreen mode

How do you run this take a look at that permits you to move a string containing areas?

For this, we have to step contained in the take a look at technique and see what it is doing. If we open the supply code for the take a look at technique, that is what it does:

def take a look at(title, &block)
  test_name = "test_#{title.gsub(/s+/, '_')}".to_sym
  outlined = method_defined? test_name
  increase "#{test_name} is already outlined in #{self}" if outlined
  if block_given?
    define_method(test_name, &block)
  else
    define_method(test_name) do
      flunk "No implementation supplied for #{title}"
    finish
  finish
finish
Enter fullscreen mode

Exit fullscreen mode

The primary line replaces all of the areas within the title string with an underscore _, and provides a take a look at originally. So, the take a look at title creating from a controller turns into test_creating_from_a_controller. Then, utilizing metaprogramming, it defines a way with that very same title. So the above take a look at technique turns into:

def test_creating_from_a_controller
  # assert ...
finish
Enter fullscreen mode

Exit fullscreen mode

Now that you already know the title, you’ll be able to run this take a look at as follows:

>  ruby -I take a look at take a look at/controller/renderer_test.rb -n test_creating_from_a_controller
Enter fullscreen mode

Exit fullscreen mode

Hope this helps.

Add a Comment

Your email address will not be published. Required fields are marked *