1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("Entering Invoke") ubytes, _ := stub.ReadCertAttribute("username") rbytes, _ := stub.ReadCertAttribute("role") username := string(ubytes) role := string(rbytes) if role != "Bank_Admin" { return nil, errors.New("caller with " + username + " and role " + role + " does not have access to invoke CreateLoanApplication") } return nil, errors.New("Invalid function name") } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | func TestInvokeFunctionValidation2(t *testing.T) { fmt.Println("Entering TestInvokeFunctionValidation2") attributes := make(map[string][]byte) attributes["username"] = []byte("vojha24") attributes["role"] = []byte("Bank_Admin") stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } _, err := stub.MockInvoke("t123", "CreateLoanApplication", []string{}) if err != nil { t.Fatalf("Expected CreateLoanApplication function to be invoked") } } |
1 Entering TestInvokeFunctionValidation2 2 2017/03/06 20:50:12 MockStub( mockStub &{} ) 3 Entering Invoke 4 --- FAIL: TestInvokeFunctionValidation2 (0.00s) 5 sample_chaincode_test.go:133 Expected CreateLoanApplication function to be invoked 6 FAIL |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("Entering Invoke") ubytes, _ := stub.ReadCertAttribute("username") rbytes, _ := stub.ReadCertAttribute("role") username := string(ubytes) role := string(rbytes) if role != "Bank_Admin" { return nil, errors.New("caller with " + username + " and role " + role + " does not have access to invoke CreateLoanApplication") } if function == "CreateLoanApplication" { return CreateLoanApplication(stub, args) } return nil, errors.New("Invalid function name. Valid functions ['CreateLoanApplication']") } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | func TestInvokeFunctionValidation2(t *testing.T) { fmt.Println("Entering TestInvokeFunctionValidation2") attributes := make(map[string][]byte) attributes["username"] = []byte("vojha24") attributes["role"] = []byte("Bank_Admin") stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } bytes, err := stub.MockInvoke("t123", "CreateLoanApplication", []string{loanApplicationID, loanApplication}) if err != nil { t.Fatalf("Expected CreateLoanApplication function to be invoked") } //A spy could have been used here to ensure CreateLoanApplication method actually got invoked. var la LoanApplication err = json.Unmarshal(bytes, &la) if err != nil { t.Fatalf("Expected valid loan application JSON string to be returned from CreateLoanApplication method") } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |