Symbiosoft
Symbiosoft

Reputation: 4711

How to use Test:Unit on a ruby file without classes (only methods)

I have to write code for an homework and I wish to do TDD from the start. The homework consists of one ruby file with methods in it, no class.

All examples I find on the Internet test against classes. How could I test the following method?

homework.rb

#!/usr/bin/env ruby

def count_words(str)
  # SOME CODE HERE
end

There is an auto-grading system that take one ruby file with the methods defined for the homework as an input. So, I have to write my tests in a separate file (test_homework.rb) or comment out my test before submitting (which I found counter productive...).

How will I test the count_words method using Test:Unit?

Upvotes: 2

Views: 958

Answers (1)

ThomasW
ThomasW

Reputation: 17317

Do something like this:

require File.join(File.expand_path(File.dirname(__FILE__)), 'homework.rb')
require "test/unit"

class TestWordCounter < Test::Unit::TestCase
  def test_count_words
    assert_equal 3, count_words("one two three")
  end
end

Upvotes: 4

Related Questions