Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing and mocking a function which does not return any value

I want to test a function, which does not return any value, but instead triggers other functions. While reading about testing, I found information that this is called a behavioral verification and that by mocking I can check what functions and in what order are triggered. However, I have a problem to implement the proper mocking technique for my code.

Let's consider a simple example of the following interface and struct (the example is very basic just to make it easier to explain):

type ExampleInterface interface {
    DoSomething(arg int)
    DoEvenMore(arg int)
    AndEvenMore(arg int)
} 

type ExampleStruct struct {
     Id string 
     // Other fields
}

func (e *ExampleStruct) DoSomething(arg int) {
     arg2 := arg * 2
     e.DoEvenMore(arg2)
     arg3 := arg * 3
     e.AndEvenMore(arg3)  
}

func (e *ExampleStruct) DoEvenMore(arg int){
     fmt.Println("I did so many operations here using ", arg)
}

func (e *ExampleStruct) AndEvenMore(arg int) {
     // Performing other operations on arg
}

Now, I want to test function DoSomething. Since it does not return any value what I want to do is to test whether after calling this function with argument 3 the following chain of events happens: function DoEvenMore is called once with argument 6, and next function AndEvenMore is called once with argument 9

I wrote the following mocking test:

func TestDoSomething(t *testing.T) {

    mockCtrl := gomock.NewController(t)
    defer mockCtrl.Finish()

    mockClient := mocks.NewMockExampleInterface(mockCtrl)

    example := ExampleStruct("ExampleId")

    gomock.InOrder(
        mockClient.EXPECT().DoSomething(3).Return().Times(1)
        mockClient.EXPECT().DoEvenMore(6).Return().Times(1)
        mockClient.EXPECT().AndEvenMore(9).Return().Times(1) 
    )

    example.DoSomething(3)
}

However, when I run this test I get the error: Unexpected call to *mocks.MockExampleInterface.DoSomething(..).

How I should properly perform the mocking in such example?

like image 883
Ziva Avatar asked Jan 03 '18 18:01

Ziva


People also ask

How do you test a function that does not return anything?

If a method is not returning anything through the "return" statement (void method), it may return data through its arguments. In this case, you can test the data returned in any argument. Else if a method is not returning any data through its arguments, it may change values of its instance variables.

What is mocks in testing?

In mock testing, the dependencies area unit is replaced with objects that simulate the behavior of the important ones. It is based upon behavior-based verification. The mock object implements the interface of the real object by creating a pseudo one. Thus, it's called mock.

How do you mock the return value of a function in Jest?

To mock a function's return value in Jest, you first need to import all named exports from a module, then use mockReturnValue on the imported function. You can use the * as <alias> inside an import statement to import all named exports. Then, you need to chain mockReturnValue off of jest.

How do you mock a function?

There are two ways to mock functions: Either by creating a mock function to use in test code, or writing a manual mock to override a module dependency.


1 Answers

The two methods called by DoSomething are concrete implementations, you cannot mock something like that in Go.

To achieve what you want DoSomething would have to depend on an interface that defines those two methods and call those instead of the concrete ones.

type DoMorer interface {
    DoEvenMore(arg int)
    AndEvenMore(arg int)
} 

type ExampleStruct struct {
     Id string 
     // Other fields
}

func (e *ExampleStruct) DoSomething(arg int, dm DoMorer) {
     arg2 := arg * 2
     dm.DoEvenMore(arg2)
     arg3 := arg * 3
     dm.AndEvenMore(arg3)  
}

With a setup like this, during testing, you can create a mock for the DoMorer interface and pass that to DoSomething.


DoMorer doesn't have to be passed to as an argument, it could be a field of ExampleStruct or a global, whatever suits your needs.

like image 149
mkopriva Avatar answered Oct 10 '22 22:10

mkopriva