smarty / assertions

Fluent assertion-style functions used by goconvey and gunit. Can also be used in any test or application.
Other
100 stars 34 forks source link

Assertions should allow an optional string with a better message #31

Closed ghostsquad closed 6 years ago

ghostsquad commented 6 years ago

This is more information that you'd normally get, but it still requires a bit of research to figure out why the assertion failed.

            (1):      /Users/wes/projects/space-z/maps/spaceship/small_test.go:31
            (0):      /Users/wes/projects/space-z/maps/spaceship/small_test.go:96
            Expected: '&{ map[] true}'
            Actual:   '&{CoEd Showers map[0:0x12838a0 1:0x12838a0 2:0xc42000a160 3:0xc42000a1c0] false}'
            (Should be equal)

It would be really nice to be able to provide my own why it failed message.

mdwhatcott commented 6 years ago

Can you provide an example of the code that would have produced this failure message?

Also, how would you achieve passing the optional string?

ghostsquad commented 6 years ago

The code is nothing more than

type Foo struct {}
type Bar struct {
   Blah string
}

this.So(Foo{}, should.Equal, Bar{})

An example of usage might be an alternative interface

this.SoBecause(Foo{}.Active, should.BeFalse, "because we don't work with old clients")
# or
this.SoBecause(Baz.Foo, should.Equal, Bar{}, "because Foo is the new client")
mdwhatcott commented 6 years ago

Ok, I see. I don't see us adding that kind of feature to gunit. Usually we extract nicely-named methods to document the purpose of a given assertion.

func (this *MyFixture) assertFooIsTheNewClient() {
    this.So(this.Baz.Foo, should.Equal, Bar{})
}

Have you tried GoConvey? It uses a more descriptive style that you might appreciate.

https://smartystreets.com/blog/2018/03/history-of-go-testing

Also, when comparing anything but primitive values, I often recommend using ShouldResemble.