github.com/s7techlab/cckit@v0.10.5/testing/expect/tx.go (about)

     1  package expect
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hyperledger/fabric-protos-go/peer"
     7  	g "github.com/onsi/gomega"
     8  )
     9  
    10  type (
    11  	Stringer interface {
    12  		String() string
    13  	}
    14  
    15  	TxRes struct {
    16  		Result interface{}
    17  		Err    error
    18  		Event  *peer.ChaincodeEvent
    19  	}
    20  )
    21  
    22  func (r *TxRes) HasError(err interface{}) *TxRes {
    23  	if err == nil {
    24  		g.Expect(r.Err).NotTo(g.HaveOccurred())
    25  	} else {
    26  		g.Expect(r.Err).To(g.HaveOccurred())
    27  		g.Expect(fmt.Sprintf(`%s`, r.Err)).To(g.ContainSubstring(fmt.Sprintf(`%s`, err)))
    28  	}
    29  	return r
    30  }
    31  
    32  func (r *TxRes) HasNoError() *TxRes {
    33  	return r.HasError(nil)
    34  }
    35  
    36  func (r *TxRes) Is(expectedResult interface{}) *TxRes {
    37  	r.HasNoError()
    38  
    39  	_, ok1 := r.Result.(Stringer)
    40  	_, ok2 := expectedResult.(Stringer)
    41  	if ok1 && ok2 {
    42  		g.Expect(r.Result.(Stringer).String()).To(g.Equal(expectedResult.(Stringer).String()))
    43  	} else {
    44  		g.Expect(r.Result).To(g.BeEquivalentTo(expectedResult))
    45  	}
    46  
    47  	return r
    48  }
    49  
    50  // ProduceEvent expects that tx produces event with particular payload
    51  func (r *TxRes) ProduceEvent(eventName string, eventPayload interface{}) *TxRes {
    52  	r.HasNoError()
    53  	EventIs(r.Event, eventName, eventPayload)
    54  	return r
    55  }