10

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Buzzy
  • 2,905
  • 3
  • 22
  • 31

4 Answers4

13

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.

Dom Corvasce
  • 201
  • 1
  • 7
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.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • 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." – Buzzy Jun 05 '17 at 10:32
  • 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. – Chris Drew Jun 09 '17 at 12:15
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")
}
jezjez
  • 31
  • 4
0

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()
}
Deep Nirmal
  • 1,141
  • 1
  • 15
  • 14