From: Ryan Davis Date: 2013-03-08T10:14:52+09:00 Subject: Re: Cannot Use Initialize With Minitest? On Mar 7, 2013, at 15:02 , James Harrison wrote: > Hey folks, > > I've been trying my best to write a class that uses initialize() with minitest, so that I can swap out a specific dependency at runtime. It appears, though, that I cannot use initialize() in a class that inherits from minitest: > > ##### > require "minitest/autorun" > > #A simple dependency which is being dispatched to. > class Dependency > def initialize(argument) > $stderr.puts argument > end > end > > #The main test class. > class TestProblemReproduction < MiniTest::Unit::TestCase > def initialize(test_argument) > Dependency.new(test_argument) > end > > def test_something > puts "This is here so minitest does something." > end > end > > > TestProblemReproduction.new("stuff") > > ####### > > Expected Output is to see the class Dependency output "stuff". > The actual output is that the class Dependency outputs "stuff\ntest_something" The #1 rule of OO is if you override a method you NEED to call super. The #2 rule of OO is if you override a method you NEED to make sure it conforms to the contract. You're breaking both these rules. #initialize is defined on TestCase, and it takes an arg which is the name of the test being run. > However, I have a use case where I would like to use minitest to run my test cases, but I have a bunch of tests written in selenium. With one flag, the selenium browser will be constructed to run locally on my desktop, for local testing. With a different flag, the selenium browser is constructed to run remotely on a set of remote test servers. > > Is there any way I can hand a dependency into my minitest based test cases? Or do they have to be static? There is nothing static about minitest. I don't see why you need to do this via initialize/new tho, so I'm guessing I don't understand the problem well enough. You shouldn't be instantiating TestCase instances. That's the runner's job.