| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library mock_test; | 5 library mock_test; |
| 6 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/mock.dart'; | 7 import 'package:unittest/mock.dart'; |
| 8 | 8 |
| 9 class MockList extends Mock implements List { | 9 class MockList extends Mock implements List { |
| 10 } | 10 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); | 56 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); |
| 57 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); | 57 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); |
| 58 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); | 58 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); |
| 59 m.getLogs(callsTo('foobar')).verify(neverHappened); | 59 m.getLogs(callsTo('foobar')).verify(neverHappened); |
| 60 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); | 60 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); |
| 61 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). | 61 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). |
| 62 verify(happenedExactly(2)); | 62 verify(happenedExactly(2)); |
| 63 expect(s, 'ACDEB'); | 63 expect(s, 'ACDEB'); |
| 64 }); | 64 }); |
| 65 | 65 |
| 66 test('Mocking: getters/setters', () { |
| 67 var m = new Mock(); |
| 68 var x = 0; |
| 69 m.when(callsTo('get foo')).alwaysReturn(3); |
| 70 m.when(callsTo('set foo')).alwaysCall((v) { x = v; }); |
| 71 m.when(callsTo('get bar')).alwaysReturn(5); |
| 72 m.when(callsTo('set bar')).alwaysCall((v) { x = 2 * v; }); |
| 73 expect(m.foo, 3); |
| 74 expect(m.bar, 5); |
| 75 m.foo = 10; |
| 76 expect(x, 10); |
| 77 m.bar = 8; |
| 78 expect(x, 16); |
| 79 }); |
| 80 |
| 66 test('Mocking: Mock List', () { | 81 test('Mocking: Mock List', () { |
| 67 var l = new MockList(); | 82 var l = new MockList(); |
| 68 l.when(callsTo('get length')).thenReturn(1); | 83 l.when(callsTo('get length')).thenReturn(1); |
| 69 l.when(callsTo('add', anything)).alwaysReturn(0); | 84 l.when(callsTo('add', anything)).alwaysReturn(0); |
| 70 l.add('foo'); | 85 l.add('foo'); |
| 71 expect(l.length, 1); | 86 expect(l.length, 1); |
| 72 | 87 |
| 73 var m = new MockList(); | 88 var m = new MockList(); |
| 74 m.when(callsTo('add', anything)).alwaysReturn(0); | 89 m.when(callsTo('add', anything)).alwaysReturn(0); |
| 75 | 90 |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 expect(log.logs, hasLength(6)); | 643 expect(log.logs, hasLength(6)); |
| 629 expect(log.logs.every((e) => e.mockName == 'm2' || e.mockName == 'm3'), | 644 expect(log.logs.every((e) => e.mockName == 'm2' || e.mockName == 'm3'), |
| 630 isTrue); | 645 isTrue); |
| 631 m2.clearLogs(); | 646 m2.clearLogs(); |
| 632 expect(log.logs, hasLength(3)); | 647 expect(log.logs, hasLength(3)); |
| 633 expect(log.logs.every((e) => e.mockName =='m3'), isTrue); | 648 expect(log.logs.every((e) => e.mockName =='m3'), isTrue); |
| 634 m3.clearLogs(); | 649 m3.clearLogs(); |
| 635 expect(log.logs, hasLength(0)); | 650 expect(log.logs, hasLength(0)); |
| 636 }); | 651 }); |
| 637 } | 652 } |
| OLD | NEW |