go-bdd / gobdd

BDD framework
https://go-bdd.github.io/gobdd/
MIT License
115 stars 19 forks source link

Add context.GetAs(target *interface{}) error #89

Closed bkielbasa closed 3 years ago

bkielbasa commented 4 years ago

When storing custom structs in the context (to avoid global state) it's common to use similar code

v, err := ctx.Get(appKey{})
if err != nil {
    t.Error(err)
    return ctx
}

app := v.(sapp.App)
app = app.Unhealthy()

The syntax would look similar to json.Unmarshal(). The above code would look like this:

var app sapp.App
err := ctx.GetAs(appKey{}, &app)
if err != nil {
    t.Error(err)
    return ctx
}

app = app.Unhealthy()