Skip to content

Instantly share code, notes, and snippets.

@Manzanit0
Created February 22, 2023 09:23
Show Gist options
  • Save Manzanit0/5a05dffceb8eb54539cfb31096c6a0b7 to your computer and use it in GitHub Desktop.
Save Manzanit0/5a05dffceb8eb54539cfb31096c6a0b7 to your computer and use it in GitHub Desktop.
Transcription of Ginkgo example with subtests
package gingko_subtests_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheckingBooksOutOfLibrary(t *testing.T) {
t.Run("when the library has the book in question", func(t *t.Testing) {
t.Run("if the book is available, it lends it to the reader", func(t *t.Testing) {
assert.Nil(t, valjean.Checkout(ctx, library, "Les Miserables"))
assert.Contains(t, valjean.Books(), book)
assert.Equal(t, library.UserWithBook(ctx, book), valjean)
})
t.Run("if the book has already been checked out, it tells the user", func(t *t.Testing) {
err := valjean.Checkout(ctx, library, "Les Miserables")
assert.Error(t, err)
assert.Equal(t, "Les Miserables is currently checked out", err)
})
t.Run("if the book has already been checked out, lets the user place a hold and get notified later", func(t *t.Testing) {
assert.Nil(t, valjean.Hold(ctx, library, "Les Miserables"))
assert.Contains(t, valjean.Holds(ctx), book)
// when Javert returns the book
assert.Nil(javert.Return(ctx, library, book))
// it eventually informs Valjean
notification := "Les Miserables is ready for pick up"
assert.Eventually(t, assert.Contains(t, valjean.Notifications, notification))
assert.Nil(t, valjean.Checkout(ctx, library, "Les Miserables"))
assert.Contains(t, valjean.Books(), book)
assert.Empty(valjean.Holds(ctx))
})
})
t.Run("when the library does not have the book in question", func(t *t.Testing) {
t.Run("it tells the reader the book is unavailable", func(t *t.Testing) {
err := valjean.Checkout(ctx, library, "Les Miserables")
assert.Error(t, err)
assert.Equal(t, "Les Miserables is not in the library catalog", err)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment