Another way is with golden files. With my test package, this would look like:
func TestIsPublishable(t *testing.T) {
testfile.Run(t, "testdata/publishable/*.json", func(t *testing.T, path string) {
var p Post
testfile.ReadJSON(t, path, &p)
be.True(t, IsPublishable(p))
})
testfile.Run(t, "testdata/not-publishable/*.json", func(t *testing.T, path string) {
var p Post
testfile.ReadJSON(t, path, &p)
be.False(t, IsPublishable(p))
})
}
The test files would then be testdata/unpublishable/same meta description and description.json and whatnot.
The good and bad of golden files is you’re capturing everything about the post. Sometimes that can lead to over-specification and test brittleness. The nice thing is you can have arbitrarily huge test files. I added a 120K line monster of JSON to a test yesterday because that was the real world data I wanted to validate, and it was fine.
This is a good way to do it.
Another way is with golden files. With my test package, this would look like:
The test files would then be
testdata/unpublishable/same meta description and description.json
and whatnot.The good and bad of golden files is you’re capturing everything about the post. Sometimes that can lead to over-specification and test brittleness. The nice thing is you can have arbitrarily huge test files. I added a 120K line monster of JSON to a test yesterday because that was the real world data I wanted to validate, and it was fine.