I can not think of a better heading.
In the following code, if rollBackLogger
is nil
, the first test case would fail but all other test cases would raise an exception.
Is there a way available to avoid this, other than using an if
statement?
I believe that this is a very common situation for unit testing and that there should be some function in assert
or some other way around to avoid this.
assert.NotNil(rollbackLogger)
assert.Equal("Action", rollBackLogger[0].Action)
assert.Equal("random path", rollBackLogger[0].FilePath)
You can simply use t.FailNow()
if you want the test to fail if the condition isn't valid.
I don't think there's a way to stop the test on an assert failure without using a condition or an external package.
if !assert.NotNil(rollbackLogger) {
t.FailNow()
}
assert.Equal("Action", rollBackLogger[0].Action)
assert.Equal("random path", rollBackLogger[0].FilePath)
or if you use the testify/assert
package,
if !assert.NotNil(rollbackLogger) {
assert.FailNow(t, "message")
}
assert.Equal("Action", rollBackLogger[0].Action)
assert.Equal("random path", rollBackLogger[0].FilePath)