1

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?

Max
  • 21,123
  • 5
  • 49
  • 71
user3483829
  • 11
  • 1
  • 3
  • I don't understand your design, but I can tell you that your testing code should be completely decoupled from your non-testing code. E.g. `Calculator` should not be including `TestModule`. – Max Oct 14 '14 at 14:41
  • I am not sure what exactly do you mean decoupled. But if I remove `TestModule` from `Calculator` I can not call methods from `TestModule` with `Calculator` object. My problem is that the module `TestModule` can not use assert methods. But I am not ruby expert and maybe my design is completely wrong. – user3483829 Oct 15 '14 at 07:28

1 Answers1

2

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
Max
  • 21,123
  • 5
  • 49
  • 71
  • Does it mean that it is not possible use module as a tested part in my structure? This is very simple example in which I wanted to describe my problem. I would like to use `minitest` for automation testing of web pages and if I will use it as you suggested then the maintenance of the framework will be very difficult and test cases will have a great amount of lines. So I would like to separate testing of results into particular methods of some module. – user3483829 Oct 19 '14 at 10:42
  • You can still put the testing methods in a module if you want, just include that module in `TestCalculator` not `Calculator`. – Max Oct 19 '14 at 14:11
  • I added 'include TestMethod` into `TestCalculator` but there are two errors now. The first is the same what I described above and the second is: `TestCalculator#test_addition:` `ArgumentError: wrong number of arguments (0 for 1)` But I have proper number of arguments. – user3483829 Oct 20 '14 at 06:18
  • I don't mean that you can simply change your includes to get it to to work. _First_ restructure your code to look like my answer. _Then_ you can put `test_addition` in a module and include it. The argument error is because methods that start with `test_` are special to Minitest. Don't name a method that unless it's part of a test class (as in my answer). – Max Oct 20 '14 at 14:10
  • I created the structure based on your answer and it works. But I mentioned it in my question after the code. This is not solution what I am looking for. I would like to send to you my specific code,is it possible? Because I feel that this way is quite long for stackoverflow usage. I will leave your possible solution here as a result. – user3483829 Oct 20 '14 at 20:01
  • You could start a [chat room](http://chat.stackoverflow.com/rooms/new) and paste the link in the comments here. Then we can work out the issue you're having. – Max Oct 21 '14 at 19:16