dsj60862 2019-03-07 20:13
浏览 45

在测试和链码之间传递瞬态数据

From what I've seen, Fabric doesn't support testing private data collections with MockStub. What I've tried to do below is workaround the lack of a transient field on MockStub by embedding MockStub within another struct.

The problem is that in the chaincode, while I can extract the transient data (stub.GetTransient()), I cannot access the needed field of my map.

Here's my struct:

type MockStubWithTransient struct {
    shim.MockStub
    transient map[string]interface{}
}

The test:

func TestCreatePOI (t *testing.T) {
    fmt.Println("Entering TestCreatePOI")
    stub := NewMockStubWithTransient("{\"poi\":{\"status\":\"unconfirmed\",\"id\":\"123\"}}")
    fmt.Println(stub)
    //  prints: &{{[] 0x158ffc0 mockStub map[] 0xc00014e960 map[]  <nil> <nil>  map[] map[] 0xc0000467e0 map[]} map[poi:map[status:unconfirmed id:123]]}
    // can access stub.transient["poi"]

    result := stub.MockInvoke("001",[][]byte{[]byte("CreatePOI")})
    fmt.Println(result)
}

func NewMockStubWithTransient(transient string) *MockStubWithTransient {
    var tMap map[string]interface{}
    _ := json.Unmarshal([]byte(transient), &tMap)
    s := shim.NewMockStub("mockStub", new(POIChaincode))
    st := &MockStubWithTransient{
        transient: tMap,
        MockStub: *s,
    }

    return st
}

When I print the stub as my chaincode sees it, I get:

&{[[67 114 101 97 116 101 80 114 111 111 102 79 102 73 110 115 117 114 97 110 99 101]] 0x158ffc0 mockStub map[] 0xc00014e960 map[] 001 seconds:1551988379 nanos:68249460  0xc0000c6190  map[] map[] 0xc0000467e0 map[]}

And I can call stub.GetTransient() but what I get is map[].

How can I populate and access the transient field of stub when stub originates in a test?

I'm not set on this method of doing things, I just need a way to unit test my chaincode that utilizes private data collections. Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据