Commit 456a597
Changed files (2)
spec
spec/spec.go
@@ -6,29 +6,39 @@ import "testing"
// because of (e.g. "with an empty body")
// it behaves like (e.g. "it returns an error message")
type Context struct {
- because func() interface{}
+ because BecauseFunc
its []ItFunc
- Result interface{}
+ state map[string]interface{}
}
type ContextFunc func(*Context)
-type BecauseFunc func() interface{}
+type BecauseFunc func()
type ItFunc func(*testing.T)
func Establish(t *testing.T, x ContextFunc) {
context := Context{
- its: []ItFunc{},
+ its: []ItFunc{},
+ because: func() {},
+ state: make(map[string]interface{}),
}
x(&context)
- context.Result = context.because()
+ context.because()
for _, it := range context.its {
it(t)
}
}
-func (c *Context) Because(b func() interface{}) {
+func (c *Context) Because(b BecauseFunc) {
c.because = b
}
func (c *Context) It(it ItFunc) {
c.its = append(c.its, it)
}
+
+func (c *Context) Set(key string, value interface{}) {
+ c.state[key] = value
+}
+
+func (c *Context) Get(key string) interface{} {
+ return c.state[key]
+}
spec/spec_test.go
@@ -7,11 +7,12 @@ import (
func TestCalculator(t *testing.T) {
t.Run("when adding 1 + 1", func(t *testing.T) {
Establish(t, func(x *Context) {
- x.Because(func() interface{} { return 1 + 1 })
+ x.Because(func() { x.Set("result", 1+1) })
x.It(func(t *testing.T) {
- if x.Result != 2 {
- t.Errorf("Expected: 2, Got: %d", x.Result)
+ result := x.Get("result").(int)
+ if result != 2 {
+ t.Errorf("Expected: 2, Got: %d", result)
}
})
})