views:

42

answers:

1

Hi.

I have a simple validation method as follows

function someMethod {
   //some processing
}

I want to unit test this method. What is the simplest way I can mock it. Usually if I have an object as follows:

var someObject = function() {
   reload : function() {
    //reload logic here
   }
}

I can stub someObject and then check if mock(someObject).should_receive("reload").exactly('once')

or something like that. But since this time I just have a method. How do I mock that?

+2  A: 

Mocking of code that is non-object oriented is difficult. Misko Hevery wrote about it here: http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability. The predicament is lack of a "hook" through which mocking stuff can be injected into the computation.

You can have the actual impl. of the method reside in an object. The method will simply delegate to that object. Thus, in order to test the method you only need to test the object (making the reasonable assumption that you got the delegation code right).

Itay