Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minitest - Tests Don't Run - No Rails

Tags:

ruby

minitest

I'm just starting a small project to emulate a Carnival's ticket sales booth and one of the guidelines was to test that a user can enter the number of tickets. The program runs in the console and I eventually (hopefully) figured it out how to implement this test thanks to @Stefan's answer on this question.

The problem is that now, when I run the test file, minitest says:

0 runs, 0 assertions, 0 failures, 0 errors, 0 skips

I get the same result when I try to run the test by name using ruby path/to/test/file.rb --name method-name. I'm not sure if this is because my code is still faulty of if it's because I've set up minitest incorrectly. I've tried to look up similar problems on SO but most questions seem to involve using minitest with rails and I just have a plain ruby project.

Here's my test file:

gem 'minitest', '>= 5.0.0'
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'carnival'

class CarnivalTest < MiniTest::Test
  def sample
    assert_equal(1, 1)
  end

  def user_can_enter_number_of_tickets
    with_stdin do |user|
      user.puts "2"
      assert_equal(Carnival.new.get_value, "2")
    end
  end

  def with_stdin
    stdin = $stdin                 # global var to remember $stdin
    $stdin, write = IO.pipe        # assign 'read end' of pipe to $stdin
    yield write                    # pass 'write end' to block
  ensure
    write.close                    # close pipe
    $stdin = stdin                 # restore $stdin
  end
end

In a file called carnival.rb in the same folder as my test file I have

Class Carnival
  def get_value
    gets.chomp
  end
end

If anyone can help figure out why the test is not running I'd be grateful!

like image 929
SoSimple Avatar asked Nov 21 '15 13:11

SoSimple


1 Answers

By convention, tests in Minitest are public instance methods that start with test_, so the original test has no actual test methods. You need to update your test class so that the methods with assertions follow the convention as:

class CarnivalTest < Minitest::Test
  def test_sample
    assert_equal(1, 1)
  end

  def test_user_can_enter_number_of_tickets
    with_stdin do |user|
      user.puts "2"
      assert_equal(Carnival.new.get_value, "2")
    end
  end

  # snip...
end
like image 64
Chris Kottom Avatar answered Nov 15 '22 04:11

Chris Kottom