stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.53k stars 1.6k forks source link

Mock object come from with previous data. Can we format previous mock data before calling it #1029

Open ahmetremziata opened 3 years ago

ahmetremziata commented 3 years ago

If there are two tests and tests are calling same mock, second test mock data come with first test data.

I show example like this.

func TestMethodA (*testing.T) {
    //Given
        var data1 := "data1"
    mockCacheService.On("GetData").Return(data1)

    //When
    resp, err := service.GetData(req)

    //Then
    assert.Nil(t, err)
    assertion.NotNil(t, resp)
}

func TestMethodB (*testing.T) {
    //Given
        var data2 := "data2"
    mockCacheService.On("GetData").Return(data2)

    //When
    resp, err := service.GetData(req)

    //Then
    assert.Nil(t, err)
    assertion.NotNil(t, resp)
}

You see that there are two tests. In second test "GetData" method come with first test data (data1). Note: If we run tests seperetaly there ara no error, but if we run tests together problem has occured.

dolmen commented 1 year ago

Please make a reproducible example running on go.dev/play. Here is a template: https://go.dev/play/p/AsRNcxCRdWx