Giter Club home page Giter Club logo

Comments (9)

fuzzybinary avatar fuzzybinary commented on August 25, 2024 4

Yeup that sounds reasonable! My recommendation would be to mock the generated gRPC client instead, since you can be reasonably sure that the generated code uses gRPC-dart correctly, and that gRPC-dart itself is tested. This makes the complicated harness completely unnecessary.

To do this using mockito:

// Assuming you're testing a generated client called LoginClient
class MockLoginClient extends Mock implements LoginClient {}

test('login request', () async {
    final mockClient = MockLoginClient();

    var expectedResp = new LoginResp();
    loginResp.token = 'Token';
    loginResp.success = true;

    when(mockClient.loginRequest(any)).thenAnswer((_) => Future.value(loginResp));

    var loginReq = new LoginReq();
    loginReq.serverAddr = '127.0.01';
    loginReq.imei = 350..;
    loginReq.language = 'EN';
    loginReq.password = 'Password';
    loginReq.userName = 'UserName';

    var realResp = await mockClient.loginRequest(loginReq);
    expect(realResp, expectedResp);
});

Does that make sense?

from grpc-dart.

PhloxDev avatar PhloxDev commented on August 25, 2024 3

Thanks for your response.

but I got this error

type 'Future' is not a subtype of type 'ResponseFuture'

Any idea how I could solve this?

from grpc-dart.

MarcinusX avatar MarcinusX commented on August 25, 2024 3

I've posted workaround for mocking client responses: google/protobuf.dart#162 (comment)

from grpc-dart.

fuzzybinary avatar fuzzybinary commented on August 25, 2024

Are you writing a test to test your own GRPC Client?

client_test.dart is really structured to test the inner workings of the client / server communication down to the Http2 transport layer. If you want to go down to this level of testing you will still have to encode the LoginResp message to a StreamMessage for this to work. The serializations are usually generated as part of the protobuf tool.

In general, if you're writing your own client you likely don't want to go down to this level, which is really testing how grpc-dart works (which already has tests).

Does that make sense?

from grpc-dart.

PhloxDev avatar PhloxDev commented on August 25, 2024

I don't want to test GRPC client.I wrote an application with dart(client side) and go(server side) and for test I don't want to connect directly to db and get response.I want to write mock server and get response from this mock server.

from grpc-dart.

fuzzybinary avatar fuzzybinary commented on August 25, 2024

It looks like ResponseFuture is an implementation detail of the client. I've opened the above issue on the actual code generator to replace it with Future in generated Client signatures.

In the meantime as a workaround you could replace ResponseFuture with Future in the generated client.

from grpc-dart.

 avatar commented on August 25, 2024

from grpc-dart.

MarcinusX avatar MarcinusX commented on August 25, 2024

Can you think of any way to work around it? To create ResponseFuture mock? I failed to do it :(

from grpc-dart.

fuzzybinary avatar fuzzybinary commented on August 25, 2024

Not that I can think of. ResponseFuture is very much an implementation detail -- its entire extension interface is private and it doesn't expose a way to create an already completed future (like Future.value).

If you attempt to mock ResponseFuture you would have to mock the relevant calls to Future, which I don't happen to know (probably just then but I'm not sure)

from grpc-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.