ExactError, asserts that err is not nil and its string matches

pull/39/head
Steve Leonard 2014-02-15 12:24:39 -05:00
parent bd93f05fd7
commit 92af479c56
3 changed files with 36 additions and 1 deletions

View File

@ -469,3 +469,22 @@ func Error(t TestingT, theError error, msgAndArgs ...interface{}) bool {
return NotNil(t, theError, "An error is expected but got nil. %s", message)
}
// Error asserts that a function returned an error (i.e. not `nil`).
//
// actualObj, err := SomeFunction()
// if assert.Error(t, err, "An error was expected") {
// assert.Equal(t, err, expectedError)
// }
//
// Returns whether the assertion was successful (true) or not (false).
func ExactError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool {
message := messageFromMsgAndArgs(msgAndArgs...)
if !NotNil(t, theError, "An error is expected but got nil. %s", message) {
return false
}
s := "An error with value \"%s\" is expected but got \"%s\". %s"
return Equal(t, theError.Error(), errString,
s, errString, theError.Error(), message)
}

View File

@ -311,6 +311,22 @@ func TestError(t *testing.T) {
}
func TestExactError(t *testing.T) {
mockT := new(testing.T)
// start with a nil error
var err error = nil
False(t, ExactError(mockT, err, ""),
"ExactError should return false for nil arg")
// now set an error
err = errors.New("Some error")
False(t, ExactError(mockT, err, "Not some error"),
"ExactError should return false for different error string")
True(t, ExactError(mockT, err, "Some error"),
"ExactError should return true")
}
func Test_isEmpty(t *testing.T) {
True(t, isEmpty(""))

View File

@ -4,7 +4,7 @@ import (
"errors"
)
// AnError is an erorr instance useful for testing. If the code does not care
// AnError is an error instance useful for testing. If the code does not care
// about error specifics, and only needs to return the error for example, this
// error should be used to make the test code more readable.
var AnError error = errors.New("assert.AnError general error for testing.")