testing.T
can also be
encapsulated in td.T
type,
simplifying again the test:
import (
"testing"
"time"
"github.com/maxatome/go-testdeep/td"
)
func TestCreateRecord(tt *testing.T) {
t := td.NewT(tt)
before := time.Now().Truncate(time.Second)
record, err := CreateRecord("Bob", 23)
if t.CmpNoError(err) {
t := t.RootName("RECORD") // Use RECORD instead of DATA in failure reports
// Using Struct shortcut method
t.Struct(record,
&Record{
Name: "Bob",
Age: 23,
},
td.StructFields{
"Id": td.NotZero(),
"CreatedAt": td.Between(before, time.Now()),
},
"Newly created record")
// Or using Cmp method, it's a matter of taste
t.Cmp(record,
td.Struct(
&Record{
Name: "Bob",
Age: 23,
},
td.StructFields{
"Id": td.NotZero(),
"CreatedAt": td.Between(before, time.Now()),
}),
"Newly created record")
}
}
Test it in playground: https://play.golang.org/p/sH4PRrVjqFM
Note the use of
RootName
method, it allows to name what we are going to test, instead of the
default “DATA”.
If CreateRecord()
had set Name
field to “Alice” value instead of
expected “Bob”, output would have been (note “RECORD” replaced default
“DATA”):