SoSimple
SoSimple

Reputation: 701

Minitest - Tests Don't Run - No Rails

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!

Upvotes: 6

Views: 2556

Answers (2)

Joseph McKenzie
Joseph McKenzie

Reputation: 87

Yeah always start all your tests with test_ so it knows that you want to that function/method

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

and that should work for you

Upvotes: -1

Chris Kottom
Chris Kottom

Reputation: 1289

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

Upvotes: 11

Related Questions