Flutter Unit Test - Expect difference between SuccessState<dynamic> and SuccessState<NetworkResponse>

Viewed 58

I'm trying to make unit test for my app but I get some issues with the expect() function.

This is my test:

test('User should register.', () async {
  final _response = await _api.register(mockSuccessfullRegisterUser);
  mockSuccessfullLoginUser = User(email: mockSuccessfullRegisterUser.email, password: mockSuccessfullRegisterUser.password);
  print(mockSuccessfullLoginUser.email);
  expect(_response.runtimeType, SuccessWithTokenResponseState);
});

My _response can return multiple types of responses like: SuccessWithTokenState, ErrorState, … The expect function gives me this error:

TestFailure (Expected: Type:<SuccessWithTokenResponseState<dynamic>>
  Actual: Type:<SuccessWithTokenResponseState<NetworkResponse>>
)

Shouldn't SuccessWithTokenResponseState be equal to SuccessWithTokenResponseState?

2 Answers

When you use a generic type without additional type it's fallback to Type<dynamic> try to use the correct type that you expect

expect(_response.runtimeType, SuccessWithTokenResponseState<NetworkResponse>);

I found the answer!

expect(_response.runtimeType is SuccessWithTokenResponseState, true);
Related