testify icon indicating copy to clipboard operation
testify copied to clipboard

Proposal: assert.Called

Open ofabricio opened this issue 3 years ago • 1 comments

Often I have to test that a function is called and I use boolean flags. One flag is alright, but when I need more things get ugly.

My suggestion is to add a new method that simplifies that, similar to the one in mock package, but in assert package, something like:

func TestExample(t *testing.T) {
    assert := assert.New(t)

    h := MyHandler(
        func() {
            assert.AddCall("Func One")
        },
        func() {
            assert.AddCall("Func Two")
        },
    )

    h(nil, nil)

    assert.Called("Func One", "Func Two")
}

ofabricio avatar Aug 19 '22 18:08 ofabricio

I don't think this should be added to the assert package. I can see what you mean, but what you are describing is a mock and there is already a mock package. The actual mock package can also make more complex assertions around the callback you are expecting such as testing arguments and returning values. This also feels a bit like trying to make your Go more terse at the cost of readability, I would prefer two bool vars in this more trivial example.

brackendawson avatar Sep 05 '22 16:09 brackendawson