Search code examples
rubytestingguardminitest

Using guard-minitest on a single Ruby file


I'm clearly doing something wrong. I'm trying to write and test plain ruby in a single file. I want guard to watch the file and the test file and run minitest any time either file changes.

So, two files: game.rb and game_test.rb

game.rb

class Game
end

game_test.rb

require 'rubygems'
require 'minitest/autorun'
require './game'

class GameTest < MiniTest::Unit::TestCase
  def test_truth
    assert true
  end
end

I also have a Guardfile that looks like this:

notification :terminal_notifier

guard 'minitest', test_folders: '.' do
  watch('game.rb')
  watch('game_test.rb')
end

Now, I'm probably forgetting something, but I can't for the life of me figure out what it is.

If I start guard and press Enter, "Run All" happens and the tests run.. at least most of the time. However, I have to press Enter for it to happen.

Also, if I make a change to the files nothing happens. I've tried putting gem 'rb-fsevent' in a Gemfile and running with "bundle exec guard" but that doesn't seem to help either.

Any help would be much appreciated. I'm going nuts.

Thanks, Jeremy


Solution

  • Your first "watch" definition will simply pass "game.rb", which is not a test file so it won't be run. The second "watch" is correct so when you save "game_test.rb", the tests should run.

    This should be a more correct Guardfile:

    notification :terminal_notifier
    
    guard 'minitest', test_folders: '.' do
      watch('game.rb') { 'game_test.rb' }
      watch('game_test.rb')
    end