f
func (t *transaction) Do(f func()) *transaction {
    if t.fail || t.rollback || t.finish {
        return t
    }
    f()
    return t
}

函数很简单,但如何测试呢,简单但丑陋的方法:

func Test_func(t *testing.T) {
    isCalled := false
    f := func() {
        isCalled = true
    }
    trans := New()
    // do something
    trans.Do(f)
    // check
    if isCalled {
        // do something
    }
}
ffDot.failt.rollbackt.finish
func Test_func(t *testing.T) {
    isCalled1 := false
    isCalled2 := false
    isCalled3 := false
    f1 := func() {
        isCalled1 = true
    }
    f2 := func() {
        isCalled2 = true
    }
    f3 := func() {
        isCalled3 = true
    }
    trans := New()
    // do something
    trans.Do(f1)
    trans.Do(f2)
    trans.Do(f3)
    // check
    if isCalled1 {
        // do something
    }
    if isCalled2 {
        // do something
    }
    if isCalled3 {
        // do something
    }
}
f1f2f3
func Test_func(t *testing.T) {
    genTestFunc := func() (func(), func() bool) {
        isCalled := false
        return func() {
                isCalled = true
            }, func() bool {
                return isCalled
            }
    }
    f1, f1Called := genTestFunc()
    f2, f2Called := genTestFunc()
    f3, f3Called := genTestFunc()
    trans := New()
    // do something
    trans.Do(f1)
    trans.Do(f2)
    trans.Do(f3)
    // check
    if f1Called() {
        // do something
    }
    if f2Called() {
        // do something
    }
    if f3Called() {
        // do something
    }
}
genTestFunc DoDof