AssertCalled always fails with testify library
Asked Answered
S

4

11

I am using testify to test my code and I want to check if a function was called.

I am doing the following:

type Foo struct {
    mock.Mock
}

func (m Foo) Bar() {

}

func TestFoo(t *testing.T) {
    m := Foo{}
    m.Bar()
    m.AssertCalled(t, "Bar")
}

The error I am getting:

Error:      Should be true
Messages:   The "Bar" method should have been called with 0 argument(s), but was not.

mock.go:419: []

I call function "Bar" and immediately ask if it was called but it returns false. What am I doing wrong? What is the proper way to test if a function was called with testify?

Schaab answered 5/6, 2017 at 8:42 Comment(0)
P
14

I tried with this and works:

type Foo struct {                                                                                                                                                    
    mock.Mock                                                                                                                                                          
}                                                                                                                                                                    

func (m *Foo) Bar() {                                                                                                                                                
    m.Called()                                                                                                                                                         
}                                                                                                                                                                    

func TestFoo(t *testing.T) {                                                                                                                                         
    m := &Foo{}                                                                                                                                                        
    m.On("Bar").Return(nil)                                                                                                                                            

    m.Bar()                                                                                                                                                            
    m.AssertCalled(t, "Bar")                                                                                                                                           
}

As stated by Chris Drew, you've to use a receiver pointer on Bar method's declaration.

Additionally, you've to istantiate a new structure as pointer and mock the method to return a value.

Pitapat answered 11/6, 2017 at 13:14 Comment(2)
Thanks, I replied on GitHub too :)Schaab
Thanks you very much!Manville
W
3

Looking at the documentation of testify I think you have to explicitly call func (*Mock) Called to tell the mock object that a method has been called.

func (m *Foo) Bar() {
    m.Called()
}

There are some examples in the testify tests.

Weatherworn answered 5/6, 2017 at 9:2 Comment(2)
I tried that before posting but it just generates the following error: "assert: mock: I don't know what to return because the method call was unexpected."Schaab
I think that might be because your method takes the receiver by value so only a copy of the mock is being updated. Try using a pointer receiver. I've updated my answer.Weatherworn
C
1

Make sure it is pointer receiver NOT value receiver.

This will always have nil Calls

type Foo struct {
    mock.Mock
}

func (m Foo) Bar() 
    m.Called()
}

This will have N number of Calls

type Foo struct {
    mock.Mock
}

func (m *Foo) Bar() 
    m.Called()
}
Careaga answered 15/7, 2021 at 5:43 Comment(0)
V
1

As an additional solution for when you want/need to use a value receiver, though not as clean, specifying Mock as a pointer field worked for me.

type Foo struct {
    m *mock.Mock
}

func (f Foo) Bar() {
    f.m.Called()
}

func TestFoo(t *testing.T) {
    f := Foo{m: &mock.Mock{}}
    f.Bar()
    f.m.AssertCalled(t, "Bar")
}
Victoria answered 16/3, 2022 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.