Skip to content

On same mock,next method mocking will override previous methods mocking if methods have similar literals #166

@AlexLuya

Description

@AlexLuya

Hello,
I have a mock like this:

type SameType struct{}
type DiffType struct{}

type ServiceMock struct{
    mock.Mock
}

func (s *ServiceMock) RetrieveHomepageOnlyData(arg0 *DiffType, arg1 * SameType) error {
    args := s.Mock.Called(arg0, arg1)


    if args.Get(0) != nil {
        return args.Error(0)
    }

    return nil
}

func (s *ServiceMock) ToCache(arg0 string, arg1 *SameType) error {
    args := s.Mock.Called(arg0, arg1)

    if args.Get(0) != nil {
        return args.Error(0)
    }

    return nil
}

As you noticed,the second args of two methods have same type,and if I mock these two methods in same test with same second arg value like this:

initDataService:=new(ServiceMock)
arg0=new(DiffType)
sameSecondValue=new(SameType)

func testMethod(){
             //given
             initDataService.On("RetrieveHomepageOnlyData", arg0, sameSecondValue).Return(nil)
             initDataService.On("ToCache","user-001", sameSecondValue).Return(nil)

             //when
             //.....

            //then
            //...
}

then only second mocking will take effect,and if running this test,I will get an error like this:

    RetrieveHomepageOnlyData(*DifferentType,*SameType)
                0: &{[] [] [] []  user-001 [] 0 [] [] [{   0  false 0 false message-002        }] [] [remind-001] [remind-001] [] session-newClient [] [{false 0    tweet-001}]  <nil>}
                1: &{ [] [] [] []  0 [] [] [] [] [] session-newClient [] 0xc20812c990 <nil>}

    The closest call I have is: 

    RetrieveHomepageOnlyData(string,*SameType)
                0: user-001
                1: &{SIGNIN [] [] [] []  0 [] [] [] [] [] session-newClient [] 0xc20812c7e0 <nil>}

Obviously,the second mocking(on method "ToCache()") is used to match first mocking(on method RetrieveHomepageOnlyData()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions