Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add testing mock #45

Closed
wants to merge 1 commit into from
Closed

add testing mock #45

wants to merge 1 commit into from

Conversation

goxiaoy
Copy link

@goxiaoy goxiaoy commented Mar 21, 2019

add mock for testing purpose

@mogol
Copy link
Collaborator

mogol commented Mar 30, 2019

Hi @goxiaoy
Why is mock implementation needed? doesn't Mockito cover this case?

@goxiaoy
Copy link
Author

goxiaoy commented Mar 30, 2019

Because when I try to do unittest within codes rely on this package,error comes with no implementation on this platform, which means on my computer

@goxiaoy
Copy link
Author

goxiaoy commented Mar 30, 2019

And it is learned from package shared_preferences

@mogol
Copy link
Collaborator

mogol commented Mar 30, 2019

Would it work in your case?

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

void main() {
  test('Sample mock', () async {
    final storage = MockStorage();
    when(storage.read(key: 'name')).thenAnswer((_) => Future.value('Stub'));

    final name = await storage.read(key: 'name');
    expect(name, 'Stub');
  });
}

class MockStorage extends Mock implements FlutterSecureStorage{}

@goxiaoy
Copy link
Author

goxiaoy commented Apr 1, 2019

It works for me. I am using dependency injection in my flutter project. so it's quite easy for me to replace FlutterSecureStorage to the mock class. But for those who did not, it's hard to replace it.

It's fine to close this PR. Thank you so much for your introduce of mockito.

@tirth-stasis
Copy link

Would it work in your case?

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

void main() {
  test('Sample mock', () async {
    final storage = MockStorage();
    when(storage.read(key: 'name')).thenAnswer((_) => Future.value('Stub'));

    final name = await storage.read(key: 'name');
    expect(name, 'Stub');
  });
}

class MockStorage extends Mock implements FlutterSecureStorage{}

How to do that for write method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants