user3483829
user3483829

Reputation: 11

NoMethodError: undefined method `assert_equal' with minitest

I have testing structure below for automation testing.

#/project/class/Calculator.rb

require 'TestModule'
require 'MathOperation'

class Calculator
  include TestModule
  include MathOperation

  def initialize(num1, num2)
    @num1 = num1
    @num2 = num2
  end
end


#/project/methods/MathOperation.rb

module MathOperation
    def operation_addition
        addition = @num1 + @num2
        return addition
    end
end


#/project/methods/TestModule.rb

module TestModule
    def test_addition(value)
       assert_equal 25, value
    end   
end


#/project/tescases/TestCalculator.rb

require 'minitest/autorun'
require 'calculator'

class TestCalculator < Minitest::Test

  def setup
    @calc = Calculator.new(15, 10)
  end

  def test_proper_addition
    resolution = @calc.operation_addition
    @calc.test_addition(resolution)
  end
end

When I execute test class TestCalculator I receive this error.

NoMethodError: undefined method 'assert_equal' for #<Calculator:0x00000002a77518 @num1=15, @num2=10

When I used assert_equal method in class TestCalculator it worked. But this way will cause in future long test cases and redundant code. How can I use "assertions" in module called by class with minitest? Is it possible?

Upvotes: 1

Views: 2642

Answers (1)

Max
Max

Reputation: 22325

The problems all come from your TestModule module. The meaning of this module is only clear if you look at all of the other code to understand it in context - this is a blatant violation of the principle of encapsulation. Why is the value 25 important? Why is the method called test_addition when the code is just asserting equality and not performing any addition? Remove that module entirely.

Then look at the examples in the minitest documentation to see the intended usage. Let Calculator do all the work, while TestCalculator does the asserting:

# no testing code here, just functionality

module MathOperation
  def operation_addition
    addition = @num1 + @num2
  end
end

class Calculator
  include MathOperation

  def initialize(num1, num2)
    @num1 = num1
    @num2 = num2
  end
end

# and now we do all of the testing stuff

require 'minitest/autorun'

class TestCalculator < Minitest::Unit::TestCase
  def setup
    @calc = Calculator.new(15, 10)
  end

  def test_addition
    assert_equal 25, @calc.operation_addition
  end
end

Upvotes: 2

Related Questions