1 2 3 4 5 6 7 8 9 | func CreateLoanApplication(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { fmt.Println("Entering CreateLoanApplication") if len(args) < 2 { fmt.Println("Invalid number of args") return nil, errors.New("Expected atleast two arguments for loan application creation") } return nil, nil } |
1 Entering TestCreateLoanApplicationValidation2 2 2017/03/06 12:07:34 MockStub( mockStub &{} ) 3 Entering CreateLoanApplication 4 PASS |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | func TestCreateLoanApplicationValidation3(t *testing.T) { fmt.Println("Entering TestCreateLoanApplicationValidation3") attributes := make(map[string][]byte) stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } stub.MockTransactionStart("t123") CreateLoanApplication(stub, []string{loanApplicationID, loanApplication}) stub.MockTransactionEnd("t123") var la LoanApplication bytes, err := stub.GetState(loanApplicationID) if err != nil { t.Fatalf("Could not fetch loan application with ID " + loanApplicationID) } err = json.Unmarshal(bytes, &la) if err != nil { t.Fatalf("Could not unmarshal loan application with ID " + loanApplicationID) } var errors = []string{} var loanApplicationInput LoanApplication err = json.Unmarshal([]byte(loanApplication), &loanApplicationInput) if la.ID != loanApplicationInput.ID { errors = append(errors, "Loan Application ID does not match") } if la.PropertyId != loanApplicationInput.PropertyId { errors = append(errors, "Loan Application PropertyId does not match") } if la.PersonalInfo.Firstname != loanApplicationInput.PersonalInfo.Firstname { errors = append(errors, "Loan Application PersonalInfo.Firstname does not match") } //Can be extended for all fields if len(errors) > 0 { t.Fatalf("Mismatch between input and stored Loan Application") for j := 0; j < len(errors); j++ { fmt.Println(errors[j]) } } } |
1 2017/03/06 18:34:38 MockStub mockStub Getting la1 () 2 --- FAIL: TestCreateLoanApplicationValidation3 (0.00s) 3 sample_chaincode_test.go:82 Could not unmarshal loan application with ID la1 4 FAIL 5 exit status 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | func CreateLoanApplication(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { fmt.Println("Entering CreateLoanApplication") if len(args) < 2 { fmt.Println("Invalid number of args") return nil, errors.New("Expected atleast two arguments for loan application creation") } var loanApplicationId = args[0] var loanApplicationInput = args[1] //TODO: Include schema validation here err := stub.PutState(loanApplicationId, []byte(loanApplicationInput)) if err != nil { fmt.Println("Could not save loan application to ledger", err) return nil, err } fmt.Println("Successfully saved loan application") return []byte(loanApplicationInput), nil } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |