Uso de Simulación Simulatoria para Probar un Código de Cadena

Este método de prueba implica el uso de una versión ficticia del stub shim.ChaincodeStubInterface. Con esto, puede simular alguna funcionalidad del código de cadenas antes de desplegarlo en Oracle Blockchain Platform. También puede utilizar esta biblioteca para crear pruebas de unidad para el código de cadenas.

  1. Cree un archivo de prueba que coincida con el nombre del archivo de código de cadena.
    Por ejemplo, si car_dealer.go es el código de implantación real para el contrato inteligente, debe crear un conjunto de pruebas denominado car_dealer_test.go que contenga todas las pruebas para car_dealer.go. El nombre de archivo del conjunto de pruebas debe tener el formato *_test.go.
  2. Cree el paquete y las sentencias de importación.
    package main
    
    import (
        "fmt"
        "testing"
    
        "github.com/hyperledger/fabric/core/chaincode/shim"
    )
  3. Cree la prueba de unidad.
    /*
    * TestInvokeInitVehiclePart simulates an initVehiclePart transaction on the CarDemo cahincode
     */
    func TestInvokeInitVehiclePart(t *testing.T) {
        fmt.Println("Entering TestInvokeInitVehiclePart")
    
        // Instantiate mockStub using CarDemo as the target chaincode to unit test
        stub := shim.NewMockStub("mockStub", new(CarDemo))
        if stub == nil {
            t.Fatalf("MockStub creation failed")
        }
    
        var serialNumber = "ser1234"
    
        // Here we perform a "mock invoke" to invoke the function "initVehiclePart" method with associated parameters
        // The first parameter is the function we are invoking
        result := stub.MockInvoke("001",
            [][]byte{[]byte("initVehiclePart"),
                []byte(serialNumber),
                []byte("tata"),
                []byte("1502688979"),
                []byte("airbag 2020"),
                []byte("aaimler ag / mercedes")})
    
        // We expect a shim.ok if all goes well
        if result.Status != shim.OK {
            t.Fatalf("Expected unauthorized user error to be returned")
        }
    
        // here we validate we can retrieve the vehiclePart object we just committed by serianNumber
        valAsbytes, err := stub.GetState(serialNumber)
        if err != nil {
            t.Errorf("Failed to get state for " + serialNumber)
        } else if valAsbytes == nil {
            t.Errorf("Vehicle part does not exist: " + serialNumber)
        }
    }

Note:

No se implementan todas las interfaces del stub. Funciones de resguardo
  • GetQueryResult

  • GetHistoryForKey

no están soportados y al intentar llamar a cualquiera de ellos se producirá un error.