func testUpdateTasksUnavailableDb(t *testing.T) { // Arrange conn, c := newClient(t) defer conn.Close() newDb := NewFakeDb(IsAvailable(false)) *fakeDb = *newDb // Clear all the fields in fakeDb.d.tasks except Id. requests := []*pb.UpdateTasksRequest{ {Id: 0}, {Id: 1}, {Id: 2}, } // Act stream, err := c.UpdateTasks(context.TODO()) if err != nil { t.Errorf("unexpected error: %v", err) } for _, req := range requests { stream.Send(req) } _, err = stream.CloseAndRecv() fakeDb.Reset() // Assert if !errorIs(err, codes.Internal, errorNoDatabaseAccess) { t.Errorf("expected Internal, got %v", err) } }
This test failed:
=== RUN TestRunAll/UpdateTasksTests/TestUpdateTasksUnavailableDb server_test.go:306: expected Internal, got <nil>
The assert statement should be after the stream.Send(req) statement, because once the request is sent, the update operation will be made on the server side.
func testUpdateTasksUnavailableDb(t *testing.T) { // Arrange conn, c := newClient(t) defer conn.Close() newDb := NewFakeDb(IsAvailable(false)) *fakeDb = *newDb // Clear all the fields in fakeDb.d.tasks except Id. requests := []*pb.UpdateTasksRequest{ {Id: 0}, {Id: 1}, {Id: 2}, } // Act stream, err := c.UpdateTasks(context.TODO()) if err != nil { t.Errorf("unexpected error: %v", err) } for _, req := range requests { if err := stream.Send(req); err != nil { // Assert if !errorIs(err, codes.Internal, errorNoDatabaseAccess) { t.Errorf("expected Internal, got %v", err) } } } _, _ = stream.CloseAndRecv() fakeDb.Reset() }
It passed after the modification:
--- PASS: TestRunAll/UpdateTasksTests/TestUpdateTasksUnavailableDb (0.00s)
标签:err,stream,Internal,req,Failure,fakeDb,Test,Id,Unit From: https://www.cnblogs.com/zhangzhihui/p/18183189