English 中文(简体)
Test modules with Test::Unit
原标题:

I encountered a problem when trying to test a module with Test::Unit. What I used to do is this:

my_module.rb:

class MyModule
  def my_func
    5 # return some value
  end
end

test_my_module.rb:

require  test/unit 
require  my_module 

class TestMyModule < Unit::Test::TestCase
  include MyModule

  def test_my_func
    assert_equal(5, my_func) # test the output value given the input params
  end
end

Now the problem is, if my_module declares an initialize method, it gets included in the test class and this causes a bunch of problems since Test::Unit seems to override/generate an initialize method. So I m wondering what is the best way to test a module?

I m also wondering wether my module should become a class at this point since the initialize method is made for initializing the state of something. Opinions?

Thanks in advance !

最佳回答

Yeah, your initialize should definitely suggest that you re going towards a class. A module in ruby often feels like an interface in other languages, as long as you implement some basic things when you include the module you ll get a lot for free.

Enumerable is a great example, as long as you define [] and each when you include Enumerable you suddenly get pop, push, etc.

So my gut feeling about testing modules is that you should probably be testing classes that include the module rather than testing the module itself unless the module is designed to not be included in anything, it s simply a code storage mechanism.

问题回答

Including an initialize method in a module feels very wrong to me, so I d rethink that at the very least.

To answer your question about testing this as a module more directly, though, I would create a new, empty class, include your module in it, create an instance of that class, and then test against that instance:

class TestClass
  include MyModule
end

class TestMyModule < Unit::Test::TestCase
  def setup
    @instance = TestClass.new
  end

  def test_my_func
    assert_equal(5, @instance.my_func) # test the output value given the input params
  end
end




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签