Skip to content

Commit

Permalink
Change async stubbing to use thenAnswer.
Browse files Browse the repository at this point in the history
Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79
  • Loading branch information
alanrussian committed Dec 12, 2017
1 parent e2192a9 commit c8583b4
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 46 deletions.
12 changes: 6 additions & 6 deletions packages/flutter_driver/test/flutter_driver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void main() {
when(mockClient.getVM()).thenReturn(mockVM);
when(mockVM.isolates).thenReturn(<VMRunnableIsolate>[mockIsolate]);
when(mockIsolate.loadRunnable()).thenReturn(mockIsolate);
when(mockIsolate.invokeExtension(any, any))
.thenReturn(makeMockResponse(<String, dynamic>{'status': 'ok'}));
when(mockIsolate.invokeExtension(any, any)).thenAnswer(
(_) => makeMockResponse(<String, dynamic>{'status': 'ok'}));
vmServiceConnectFunction = (String url) {
return new Future<VMServiceClientConnection>.value(
new VMServiceClientConnection(mockClient, mockPeer)
Expand Down Expand Up @@ -78,7 +78,7 @@ void main() {

test('connects to isolate paused mid-flight', () async {
when(mockIsolate.pauseEvent).thenReturn(new MockVMPauseBreakpointEvent());
when(mockIsolate.resume()).thenReturn(new Future<Null>.value());
when(mockIsolate.resume()).thenAnswer((_) => new Future<Null>.value());

final FlutterDriver driver = await FlutterDriver.connect(dartVmServiceUrl: '');
expect(driver, isNotNull);
Expand Down Expand Up @@ -124,14 +124,14 @@ void main() {
});

test('checks the health of the driver extension', () async {
when(mockIsolate.invokeExtension(any, any)).thenReturn(
makeMockResponse(<String, dynamic>{'status': 'ok'}));
when(mockIsolate.invokeExtension(any, any)).thenAnswer(
(_) => makeMockResponse(<String, dynamic>{'status': 'ok'}));
final Health result = await driver.checkHealth();
expect(result.status, HealthStatus.ok);
});

test('closes connection', () async {
when(mockClient.close()).thenReturn(new Future<Null>.value());
when(mockClient.close()).thenAnswer((_) => new Future<Null>.value());
await driver.close();
});

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/test/base/io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
final ProcessSignal signalUnderTest = new ProcessSignal(mockSignal);
final StreamController<io.ProcessSignal> controller = new StreamController<io.ProcessSignal>();

when(mockSignal.watch()).thenReturn(controller.stream);
when(mockSignal.watch()).thenAnswer((_) => controller.stream);
controller.add(mockSignal);

expect(signalUnderTest, await signalUnderTest.watch().first);
Expand All @@ -31,4 +31,4 @@ void main() {
});
}

class MockIoProcessSignal extends Mock implements io.ProcessSignal {}
class MockIoProcessSignal extends Mock implements io.ProcessSignal {}
2 changes: 1 addition & 1 deletion packages/flutter_tools/test/commands/drive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void main() {
testUsingContext('uses existing simulator', () async {
withMockDevice();
when(mockDevice.name).thenReturn('mock-simulator');
when(mockDevice.isLocalEmulator).thenReturn(new Future<bool>.value(true));
when(mockDevice.isLocalEmulator).thenAnswer((_) => new Future<bool>.value(true));

final Device device = await findTargetDevice();
expect(device.name, 'mock-simulator');
Expand Down
24 changes: 13 additions & 11 deletions packages/flutter_tools/test/compile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ void main() {
mockFrontendServerStdIn = new MockStdIn();
mockFrontendServerStdErr = new MockStream();

when(mockFrontendServer.stderr).thenReturn(mockFrontendServerStdErr);
when(mockFrontendServer.stderr).thenAnswer((_) => mockFrontendServerStdErr);
final StreamController<String> stdErrStreamController = new StreamController<String>();
when(mockFrontendServerStdErr.transform<String>(any)).thenReturn(stdErrStreamController.stream);
when(mockFrontendServer.stdin).thenReturn(mockFrontendServerStdIn);
when(mockProcessManager.start(any)).thenReturn(new Future<Process>.value(mockFrontendServer));
when(mockProcessManager.start(any)).thenAnswer(
(_) => new Future<Process>.value(mockFrontendServer));
when(mockFrontendServer.exitCode).thenReturn(0);
});

testUsingContext('single dart successful compilation', () async {
final BufferLogger logger = context[Logger];
when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockFrontendServer.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'result abc\nline1\nline2\nabc /path/to/main.dart.dill'
))
Expand All @@ -55,7 +56,7 @@ void main() {
testUsingContext('single dart failed compilation', () async {
final BufferLogger logger = context[Logger];

when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockFrontendServer.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'result abc\nline1\nline2\nabc'
))
Expand Down Expand Up @@ -88,20 +89,21 @@ void main() {
mockFrontendServerStdErr = new MockStream();

when(mockFrontendServer.stdin).thenReturn(mockFrontendServerStdIn);
when(mockFrontendServer.stderr).thenReturn(mockFrontendServerStdErr);
when(mockFrontendServer.stderr).thenAnswer((_) => mockFrontendServerStdErr);
stdErrStreamController = new StreamController<String>();
when(mockFrontendServerStdErr.transform<String>(any)).thenReturn(stdErrStreamController.stream);
when(mockFrontendServerStdErr.transform<String>(any))
.thenAnswer((_) => stdErrStreamController.stream);

when(mockProcessManager.start(any)).thenReturn(
new Future<Process>.value(mockFrontendServer)
when(mockProcessManager.start(any)).thenAnswer(
(_) => new Future<Process>.value(mockFrontendServer)
);
when(mockFrontendServer.exitCode).thenReturn(0);
});

testUsingContext('single dart compile', () async {
final BufferLogger logger = context[Logger];

when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockFrontendServer.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'result abc\nline1\nline2\nabc /path/to/main.dart.dill'
))
Expand All @@ -122,7 +124,7 @@ void main() {
final BufferLogger logger = context[Logger];

final StreamController<List<int>> streamController = new StreamController<List<int>>();
when(mockFrontendServer.stdout).thenReturn(streamController.stream);
when(mockFrontendServer.stdout).thenAnswer((_) => streamController.stream);
streamController.add(UTF8.encode('result abc\nline0\nline1\nabc /path/to/main.dart.dill\n'));
await generator.recompile('/path/to/main.dart', null /* invalidatedFiles */);
expect(mockFrontendServerStdIn.getAndClear(), 'compile /path/to/main.dart\n');
Expand All @@ -144,7 +146,7 @@ void main() {
final BufferLogger logger = context[Logger];

final StreamController<List<int>> streamController = new StreamController<List<int>>();
when(mockFrontendServer.stdout).thenReturn(streamController.stream);
when(mockFrontendServer.stdout).thenAnswer((_) => streamController.stream);
streamController.add(UTF8.encode(
'result abc\nline0\nline1\nabc /path/to/main.dart.dill\n'
));
Expand Down
30 changes: 15 additions & 15 deletions packages/flutter_tools/test/ios/code_signing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ void main() {

when(mockProcessManager.start(
argThat(contains('openssl')), environment: any, workingDirectory: any,
)).thenReturn(new Future<Process>.value(mockProcess));
)).thenAnswer((_) => new Future<Process>.value(mockProcess));

when(mockProcess.stdin).thenReturn(mockStdIn);
when(mockProcess.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockProcess.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'subject= /CN=iPhone Developer: Profile 1 (1111AAAA11)/OU=3333CCCC33/O=My Team/C=US'
))
));
when(mockProcess.stderr).thenReturn(mockStdErr);
when(mockProcess.stderr).thenAnswer((_) => mockStdErr);
when(mockProcess.exitCode).thenReturn(0);

final String developmentTeam = await getCodeSigningIdentityDevelopmentTeam(iosApp: app);
Expand Down Expand Up @@ -178,15 +178,15 @@ void main() {

when(mockProcessManager.start(
argThat(contains('openssl')), environment: any, workingDirectory: any,
)).thenReturn(new Future<Process>.value(mockOpenSslProcess));
)).thenAnswer((_) => new Future<Process>.value(mockOpenSslProcess));

when(mockOpenSslProcess.stdin).thenReturn(mockOpenSslStdIn);
when(mockOpenSslProcess.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockOpenSslProcess.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'subject= /CN=iPhone Developer: Profile 3 (3333CCCC33)/OU=4444DDDD44/O=My Team/C=US'
))
));
when(mockOpenSslProcess.stderr).thenReturn(mockOpenSslStdErr);
when(mockOpenSslProcess.stderr).thenAnswer((_) => mockOpenSslStdErr);
when(mockOpenSslProcess.exitCode).thenReturn(0);

final String developmentTeam = await getCodeSigningIdentityDevelopmentTeam(iosApp: app);
Expand Down Expand Up @@ -247,15 +247,15 @@ void main() {

when(mockProcessManager.start(
argThat(contains('openssl')), environment: any, workingDirectory: any,
)).thenReturn(new Future<Process>.value(mockOpenSslProcess));
)).thenAnswer((_) => new Future<Process>.value(mockOpenSslProcess));

when(mockOpenSslProcess.stdin).thenReturn(mockOpenSslStdIn);
when(mockOpenSslProcess.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockOpenSslProcess.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'subject= /CN=iPhone Developer: Profile 1 (1111AAAA11)/OU=5555EEEE55/O=My Team/C=US'
)),
));
when(mockOpenSslProcess.stderr).thenReturn(mockOpenSslStdErr);
when(mockOpenSslProcess.stderr).thenAnswer((_) => mockOpenSslStdErr);
when(mockOpenSslProcess.exitCode).thenReturn(0);

final String developmentTeam = await getCodeSigningIdentityDevelopmentTeam(iosApp: app, usesTerminalUi: false);
Expand Down Expand Up @@ -308,15 +308,15 @@ void main() {

when(mockProcessManager.start(
argThat(contains('openssl')), environment: any, workingDirectory: any,
)).thenReturn(new Future<Process>.value(mockOpenSslProcess));
)).thenAnswer((_) => new Future<Process>.value(mockOpenSslProcess));

when(mockOpenSslProcess.stdin).thenReturn(mockOpenSslStdIn);
when(mockOpenSslProcess.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockOpenSslProcess.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'subject= /CN=iPhone Developer: Profile 3 (3333CCCC33)/OU=4444DDDD44/O=My Team/C=US'
))
));
when(mockOpenSslProcess.stderr).thenReturn(mockOpenSslStdErr);
when(mockOpenSslProcess.stderr).thenAnswer((_) => mockOpenSslStdErr);
when(mockOpenSslProcess.exitCode).thenReturn(0);
when(mockConfig.getValue('ios-signing-cert')).thenReturn('iPhone Developer: Profile 3 (3333CCCC33)');

Expand Down Expand Up @@ -376,15 +376,15 @@ void main() {

when(mockProcessManager.start(
argThat(contains('openssl')), environment: any, workingDirectory: any,
)).thenReturn(new Future<Process>.value(mockOpenSslProcess));
)).thenAnswer((_) => new Future<Process>.value(mockOpenSslProcess));

when(mockOpenSslProcess.stdin).thenReturn(mockOpenSslStdIn);
when(mockOpenSslProcess.stdout).thenReturn(new Stream<List<int>>.fromFuture(
when(mockOpenSslProcess.stdout).thenAnswer((_) => new Stream<List<int>>.fromFuture(
new Future<List<int>>.value(UTF8.encode(
'subject= /CN=iPhone Developer: Profile 3 (3333CCCC33)/OU=4444DDDD44/O=My Team/C=US'
))
));
when(mockOpenSslProcess.stderr).thenReturn(mockOpenSslStdErr);
when(mockOpenSslProcess.stderr).thenAnswer((_) => mockOpenSslStdErr);
when(mockOpenSslProcess.exitCode).thenReturn(0);
when(mockConfig.getValue('ios-signing-cert')).thenReturn('iPhone Developer: Invalid Profile');

Expand Down
12 changes: 7 additions & 5 deletions packages/flutter_tools/test/ios/devices_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void main() {

testUsingContext('returns no devices if none are attached', () async {
when(iMobileDevice.isInstalled).thenReturn(true);
when(iMobileDevice.getAvailableDeviceIDs()).thenReturn(new Future<String>.value(''));
when(iMobileDevice.getAvailableDeviceIDs()).thenAnswer((_) => new Future<String>.value(''));
final List<IOSDevice> devices = await IOSDevice.getAttachedDevices();
expect(devices, isEmpty);
}, overrides: <Type, Generator>{
Expand All @@ -53,7 +53,7 @@ void main() {

testUsingContext('returns attached devices', () async {
when(iMobileDevice.isInstalled).thenReturn(true);
when(iMobileDevice.getAvailableDeviceIDs()).thenReturn(new Future<String>.value('''
when(iMobileDevice.getAvailableDeviceIDs()).thenAnswer((_) => new Future<String>.value('''
98206e7a4afd4aedaff06e687594e089dede3c44
f577a7903cc54959be2e34bc4f7f80b7009efcf4
'''));
Expand Down Expand Up @@ -82,16 +82,18 @@ f577a7903cc54959be2e34bc4f7f80b7009efcf4
testUsingContext('suppresses non-Flutter lines from output', () async {
when(mockIMobileDevice.startLogger()).thenAnswer((_) {
final Process mockProcess = new MockProcess();
when(mockProcess.stdout).thenReturn(new Stream<List<int>>.fromIterable(<List<int>>['''
when(mockProcess.stdout)
.thenAnswer((_) => new Stream<List<int>>.fromIterable(<List<int>>['''
Runner(Flutter)[297] <Notice>: A is for ari
Runner(libsystem_asl.dylib)[297] <Notice>: libMobileGestalt MobileGestaltSupport.m:153: pid 123 (Runner) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
Runner(libsystem_asl.dylib)[297] <Notice>: libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)
Runner(Flutter)[297] <Notice>: I is for ichigo
Runner(UIKit)[297] <Notice>: E is for enpitsu"
'''.codeUnits]));
when(mockProcess.stderr).thenReturn(const Stream<List<int>>.empty());
when(mockProcess.stderr).thenAnswer((_) => const Stream<List<int>>.empty());
// Delay return of exitCode until after stdout stream data, since it terminates the logger.
when(mockProcess.exitCode).thenReturn(new Future<int>.delayed(Duration.ZERO, () => 0));
when(mockProcess.exitCode)
.thenAnswer((_) => new Future<int>.delayed(Duration.ZERO, () => 0));
return new Future<Process>.value(mockProcess);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/test/ios/mac_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void main() {
testUsingContext('idevicescreenshot captures and returns screenshot', () async {
when(mockOutputFile.path).thenReturn(outputPath);
when(mockProcessManager.run(any, environment: null, workingDirectory: null))
.thenReturn(new Future<ProcessResult>.value(new ProcessResult(4, 0, '', '')));
.thenAnswer((_) => new Future<ProcessResult>.value(new ProcessResult(4, 0, '', '')));

await iMobileDevice.takeScreenshot(mockOutputFile);
verify(mockProcessManager.run(<String>['idevicescreenshot', outputPath],
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_tools/test/ios/simulators_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void main() {
// Let everything else return exit code 0 so process.dart doesn't crash.
when(
mockProcessManager.run(any, environment: null, workingDirectory: null)
).thenReturn(
).thenAnswer((_) =>
new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
);
// Doesn't matter what the device is.
Expand Down Expand Up @@ -206,7 +206,7 @@ void main() {
setUp(() {
mockProcessManager = new MockProcessManager();
when(mockProcessManager.start(any, environment: null, workingDirectory: null))
.thenReturn(new Future<Process>.value(new MockProcess()));
.thenAnswer((_) => new Future<Process>.value(new MockProcess()));
});

testUsingContext('uses tail on iOS versions prior to iOS 11', () async {
Expand Down Expand Up @@ -240,7 +240,7 @@ void main() {
setUp(() {
mockProcessManager = new MockProcessManager();
when(mockProcessManager.start(any, environment: null, workingDirectory: null))
.thenReturn(new Future<Process>.value(new MockProcess()));
.thenAnswer((_) => new Future<Process>.value(new MockProcess()));
});

testUsingContext('uses tail on iOS versions prior to iOS 11', () async {
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/test/stop_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
final StopCommand command = new StopCommand();
applyMocksToCommand(command);
final MockAndroidDevice device = new MockAndroidDevice();
when(device.stopApp(any)).thenReturn(new Future<bool>.value(true));
when(device.stopApp(any)).thenAnswer((_) => new Future<bool>.value(true));
testDeviceManager.addDevice(device);
await createTestCommandRunner(command).run(<String>['stop']);
});
Expand All @@ -32,7 +32,7 @@ void main() {
final StopCommand command = new StopCommand();
applyMocksToCommand(command);
final MockIOSDevice device = new MockIOSDevice();
when(device.stopApp(any)).thenReturn(new Future<bool>.value(true));
when(device.stopApp(any)).thenAnswer((_) => new Future<bool>.value(true));
testDeviceManager.addDevice(device);

await createTestCommandRunner(command).run(<String>['stop']);
Expand Down

0 comments on commit c8583b4

Please sign in to comment.