Check if class was instantiated and instance method was called
Hey guys! I'd like to figure out a way to test piece of code that suppose to create an instance of particular class. Here is an example:
class Subject def call ... end end class Executor def action Subject.new.call end end class ExecutorTest < Minitest::Test def test_subject_was_instantiated # How to ensure that Subject instance was created? end def test_subject_was_called # How to ensure that Executor send :call message to Subject instance? end end
I guess, I'm missing some concept or testing pattern here. Any advice?
1. To check if an instance method was called, it is possible to use a mock. For example, Minites::Mock.
2. To make sure a piece of code (Executor#action in the example above) performs a call to a class/instance method (Subject#call), it is possible to replace target method with a stub. Minitest library provides Object#stub method for this.
3. Testing logic can be simplified if Executor will be implemented with dependency injection in mind. So it relation between Executor and Subject will not be hardcoded, but configurable in runtime.