AssertCalled always fails with testify library

I am using testify to inspect my code and I want to check if a function has been 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 the "Bar" function and immediately ask if it was called, but it returns false. What am I doing wrong? What is the correct way to check if a function was called with an indication?

+3


source to share


2 answers


I tried with this and it worked:

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 must use a getter pointer in your Bar method declaration.

Also, you must initialize the new structure as a pointer and mock the method to return the value.

+2


source


Looking at the testify documentation I think you need to explicitly call func (*Mock) Called

to tell the mock that the method has been called.

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

      



There are several examples in testify tests .

0


source







All Articles