TATN
TATN

Reputation: 1269

Unit Test with Ruby 1.9.3

I just started learning Ruby last week, so please bear with me on this.

I'm using Ruby 1.9.3. I have problem trying to get my unit test to run, and I've been trying for the past 2 days, but I haven't been able to successfully run my unit test.

Here's the code for my simple calculator class:

class Calc
def initialize
end

def Add(leftOp, rightOp)
    return (leftOp + rightOp)
end

def Sub(leftOp, rightOp)
    return (leftOp - rightOp)
end

def Div(leftOp, rightOp)
    if(rightOp == 0)
        raise ArgumentError.new("Divided by 0")
    else
        return (leftOp / rightOp)
    end

    def Mult(leftOp, rightOp)
        return (leftOp * rightOp)
    end
end

myCalc = Calc.new
puts "2 + 3 = " + myCalc.Add(2, 3).to_s
puts "2 - 3 = " + myCalc.Sub(2, 3).to_s
puts "2 * 3 = " + myCalc.Mult(2, 3).to_s
puts "6 / 3 = " + myCalc.Div(6, 3).to_s
puts "10 / 0 = " + myCalc.Div(10, 0).to_s

This works fine. All the outputs are printed out as expected. Now, I want to write a unit test for this class. Below is the code for the unit test:

require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'Calc'

class Test_Calc < Test::Unit::TestCase
    def setup
        myCalc = Calc.new
        @result_1 = myCalc.Add(5, 8)
        @result_2 = myCalc.Sub(9, 15)
        @result_3 = myCalc.Mult(8, 6)
        @result_4 = myCalc.Div(18, 3)
        @result_5 = myCalc.Div(2, 0)
    end

    def test_Add
        assert_equal(@result_1, 13)
    end

    def test_Sub
        assert_equal(@result_2, -6)
    end

    def test_Mult
        assert_equal(@result_3, 48)
    end

    def test_Div
        assert_equal(@result_4, 6)
    end

    def test_Div_Zero
        assert_match("\Divided by 0\", e.message)
    end
end

Test::Unit::UI::Console::TestRunner.run(Test_Calc)

When I ran this unit test, it spit out this error

enter image description here

The Calc.rb file is in the very same directory of the test_Calc.rb file. Why doesn't it see it? I even tried to copy the Calc.rb file to the same directory of the "custom_require.rb" file ("lib/ruby/site_ruby/1.9.1/rubygems"), but that didn't help.

After searching all over the web, I learned that Test::Unit has been removed and replaced by MiniTest (!). So, I tried the MiniTest thing as followed:

require 'minitest/unit'
require 'Calc'
class Test_Calc < MiniTest::Unit::TestCase
    #Test_Calc definition
end
MiniTest::Unit.autorun

I still got back the exact same error as before, so maybe I was doing something wrong with the MiniTest thing.

I even followed the example from the following site, but it didn't seem to work in my case. Even if I have a fully defined class (Calc.rb), I still got the "Calc - (LoadError)".

http://www.ibm.com/developerworks/opensource/tutorials/os-ruby1/section3.html

After searching all over the web again, I learned that test::unit was available as a gem for backward compatibility, and so I downloaded and install test-unit-2.5.4.gem from the link below:

http://rubygems.org/gems/test-unit

Even after installing the gem, it still didn't work (I checked to ensure that the gem was installed in "Ruby193\lib\ruby\gem\1.9.1\gems"). Ruby still complained that it "cannot load such file -- Calc (LoadError)"

So, how do I run the test unit? What am I missing?

Thank you in advance for your help.

Updated:

After including ".\Calc" as Blaine suggested, I got the following new error message:

enter image description here

Well, at least, it recognizes the Calc class this time, and it started loading the test suite.

Upvotes: 2

Views: 2353

Answers (1)

BlackHatSamurai
BlackHatSamurai

Reputation: 23503

First, if the program says it can't find the file, make your require look like this: require './Calc'

Second, make sure you are using the latest version of the minitest gem. You can check this by doing:

gem list

The version will be on the right of the list. As of today the current version is 4.6.1. If your version is not correct, update it by doing:

gem install --version '4.6.1' (or whatever the version is)

Upvotes: 3

Related Questions