unit testing, how far to take it when black box testing?

Greetings,

I ran into a odd question.

I believe when testing code, you drop in parameters x, y, z into a function, you need to test the output to insure that the work was done properly. But what if your code uses a facade pattern or contacts an "internal" source that you can query, but that would quickly causes tight coupling issues in the test code.

Here is a example:

class Notifier

  def send_notification action_string
    # contact messaging server and deliver message
  end

end

class DoSpecialStuff

  def my_method
    code.. code..
    n = Notifier.new
    n.send_notification "WOOT"
  end

end

Now, DoSpecialStuff.my_method doens't have any output, but the Notifier which is meant to abstract the "complexity" of where the output goes! Thus resulting in an untestable function!

Do we test this code by doing this?

def test
  assert_no_execption_raised do # hurm... a test in name only?
    o = DoSpecialStuff.new
    o.my_method
  end
end

Now, we can check where this message goes, but that takes away from the point of the facade object, we're trying to hide complexity.

We can test the output, but should we? What is right and proper?

-daniel

Answers


It helps to always keep in mind that the point of testing is to give you confidence that your code works, and will continue to work in the future.

This looks like the sort of place where interaction testing would be useful. Mock out the Notifier in DoSpecialStuff's test so you can be sure it's using the Notifier correctly. Higher up you may need to mock out DoSpecialStuff to test code that sits on top of it.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.