英文:
How to write a unit test for hyperledger/fabric-sdk-go?
问题
以下是上述代码的单元测试示例:
import (
"context"
"encoding/json"
"testing"
)
func TestGetBook(t *testing.T) {
// 创建一个模拟的上下文
ctx := context.Background()
// 创建一个模拟的服务实例
svc := &service{
contract: // 这里填入你的模拟合约实例,
}
// 调用 GetBook 函数
id := "your_book_id"
entity, err := svc.GetBook(ctx, id)
// 检查返回的错误
if err != nil {
t.Errorf("GetBook returned an error: %v", err)
}
// 检查返回的实体是否符合预期
expectedEntity := &Entity{
// 这里填入你期望的实体属性值
}
if !reflect.DeepEqual(entity, expectedEntity) {
t.Errorf("GetBook returned unexpected entity. Expected: %v, Got: %v", expectedEntity, entity)
}
}
请注意,上述示例中的一些部分需要根据你的实际情况进行修改,比如模拟的合约实例和期望的实体属性值。另外,你可能还需要导入一些其他的包,以便在测试中使用。
英文:
How to write a unit test for the following code
The GetBook function is a function in chaincode code
func (svc *service) GetBook(_ context.Context, id string) (*Entity, error) {
res, err := svc.contract.EvaluateTransaction("GetBook", id)
if err != nil {
return nil, fmt.Errorf("error on evaluate transaction: %w", err)
}
var rsp Entity
err = json.Unmarshal(res, &rsp)
if err != nil {
return nil, fmt.Errorf("error on unmarshal json: %w", err)
}
return &rsp, nil
}
答案1
得分: 1
要运行链码的单元测试,您需要使用counterfeiter,它可以用来生成contractapi和chaincodeStubs的所有模拟接口。这些代码可以放在您的xxxTest.go文件中。
//go:generate counterfeiter -o mocks/transaction.go -fake-name TransactionContext . transactionContext
type transactionContext interface {
contractapi.TransactionContextInterface
}
//go:generate counterfeiter -o mocks/chaincodestub.go -fake-name ChaincodeStub . chaincodeStub
type chaincodeStub interface {
shim.ChaincodeStubInterface
}
并且导入生成的模拟接口以编写单元测试。
func TestReadCashWallet(t *testing.T) {
chaincodeStub := &mocks.ChaincodeStub{}
transactionContext := &mocks.TransactionContext{}
transactionContext.GetStubReturns(chaincodeStub)
cashWalletContract := chaincode.CashWalletContract{}
cashwallet := &chaincode.CashWallet{
ID: "id",
}
bytes, err := json.Marshal(cashwallet)
require.NoError(t, err, "error json marshal")
chaincodeStub.GetStateReturns(bytes, nil)
res, err := cashWalletContract.ReadCashWallet(transactionContext, "id")
require.EqualValues(t, cashwallet, res)
}
您需要模拟所有shim函数的操作,例如GetState、GetHistoryForKey、PutState等。
更详细的示例可以在fabric-samples的这里找到。您可以克隆它并查看asset-transfer-private-data/chaincode-go中使用counterfeiter和单元测试示例的完整代码示例。
英文:
To run unit test for chaincode, you need to use counterfeiter which you can use to generate all the mock interfaces of contractapi and chaincodeStubs. This can go in your xxxTest.go file
//go:generate counterfeiter -o mocks/transaction.go -fake-name TransactionContext . transactionContext
type transactionContext interface {
contractapi.TransactionContextInterface
}
//go:generate counterfeiter -o mocks/chaincodestub.go -fake-name ChaincodeStub . chaincodeStub
type chaincodeStub interface {
shim.ChaincodeStubInterface
}
and import the generated mock interfaces to write unit-test
func TestReadCashWallet(t *testing.T) {
chaincodeStub := &mocks.ChaincodeStub{}
transactionContext := &mocks.TransactionContext{}
transactionContext.GetStubReturns(chaincodeStub)
cashWalletContract := chaincode.CashWalletContract{}
cashwallet := &chaincode.CashWallet{
ID: "id",
}
bytes, err := json.Marshal(cashwallet)
require.NoError(t, err, "error json marshal")
chaincodeStub.GetStateReturns(bytes, nil)
res, err := cashWalletContract.ReadCashWallet(transactionContext, "id")
require.EqualValues(t, cashwallet, res)
}
You have to mock the all the shim functions actions such as GetState, GetHistoryForKey, PutState etc.
More detailed examples are provided in the fabric-samples here. You can clone this and check out the full code example with the usage of counterfeiter and unit test example in the asset-transfer-private-data/chaincode-go
答案2
得分: -1
import (
"context"
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
)
func (svc *service) GetBook(_ context.Context, id string) (*Entity, error) {
res, err := svc.contract.EvaluateTransaction("GetBook", id)
if err != nil {
return nil, fmt.Errorf("在评估事务时发生错误:%w", err)
}
var rsp Entity
err = json.Unmarshal(res, &rsp)
if err != nil {
return nil, fmt.Errorf("在解析 JSON 时发生错误:%w", err)
}
return &rsp, nil
}
以上是要翻译的代码部分。
英文:
import (
"context"
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
)
func (svc *service) GetBook(_ context.Context, id string) (*Entity, error) {
res, err := svc.contract.EvaluateTransaction("GetBook", id)
if err != nil {
return nil, fmt.Errorf("error on evaluate transaction: %w", err)
}
var rsp Entity
err = json.Unmarshal(res, &rsp)
if err != nil {
return nil, fmt.Errorf("error on unmarshal json: %w", err)
}
return &rsp, nil
}```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论