| 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 |
| 7 import 'package:unittest/mock.dart'; | 7 import 'package:unittest/unittest.dart' show test, group, skip_test; |
| 8 import 'package:matcher/matcher.dart'; |
| 9 import 'package:mock/mock.dart'; |
| 8 | 10 |
| 9 class MockList extends Mock implements List { | 11 class MockList extends Mock implements List { |
| 10 dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 12 dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 11 } | 13 } |
| 12 | 14 |
| 13 class Foo { | 15 class Foo { |
| 14 sum(a, b, c) => a + b + c; | 16 sum(a, b, c) => a + b + c; |
| 15 } | 17 } |
| 16 | 18 |
| 17 class FooSpy extends Mock implements Foo { | 19 class FooSpy extends Mock implements Foo { |
| 18 Foo real; | 20 final Foo real = new Foo(); |
| 21 |
| 19 FooSpy() { | 22 FooSpy() { |
| 20 real = new Foo(); | |
| 21 this.when(callsTo('sum')).alwaysCall(real.sum); | 23 this.when(callsTo('sum')).alwaysCall(real.sum); |
| 22 } | 24 } |
| 23 | 25 |
| 24 dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 26 dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 25 } | 27 } |
| 26 | 28 |
| 27 makeTestLogEntry(String methodName, List args, int time, | 29 LogEntry makeTestLogEntry(String methodName, List args, int time, |
| 28 [String mockName]) { | 30 [String mockName]) { |
| 29 LogEntry e = new LogEntry(mockName, methodName, args, Action.IGNORE); | 31 LogEntry e = new LogEntry(mockName, methodName, args, Action.IGNORE); |
| 30 e.time = new DateTime.fromMillisecondsSinceEpoch(time, isUtc: true); | 32 e.time = new DateTime.fromMillisecondsSinceEpoch(time, isUtc: true); |
| 31 return e; | 33 return e; |
| 32 } | 34 } |
| 33 | 35 |
| 34 makeTestLog() { | 36 LogEntryList makeTestLog() { |
| 35 LogEntryList logList = new LogEntryList('test'); | 37 var args = new List(); |
| 36 List args = new List(); | 38 return new LogEntryList('test') |
| 37 logList.add(makeTestLogEntry('a', args, 1000)); | 39 ..add(makeTestLogEntry('a', args, 1000)) |
| 38 logList.add(makeTestLogEntry('b', args, 2000)); | 40 ..add(makeTestLogEntry('b', args, 2000)) |
| 39 logList.add(makeTestLogEntry('c', args, 3000)); | 41 ..add(makeTestLogEntry('c', args, 3000)); |
| 40 return logList; | |
| 41 } | 42 } |
| 42 | 43 |
| 43 main() { | 44 void main() { |
| 44 test('Mocking: Basics', () { | 45 test('Mocking: Basics', () { |
| 45 var m = new Mock(); | 46 var m = new Mock(); |
| 46 print(m.length); | 47 // intentional no-opp access to m.length |
| 48 var foo = m.length; |
| 47 m.getLogs(callsTo('get length')).verify(happenedOnce); | 49 m.getLogs(callsTo('get length')).verify(happenedOnce); |
| 48 | 50 |
| 49 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); | 51 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); |
| 50 m.when(callsTo('foo', 1, 1)).thenReturn('C'); | 52 m.when(callsTo('foo', 1, 1)).thenReturn('C'); |
| 51 m.when(callsTo('foo', 9, anything)).thenReturn('D'); | 53 m.when(callsTo('foo', 9, anything)).thenReturn('D'); |
| 52 m.when(callsTo('bar', anything, anything)).thenReturn('E'); | 54 m.when(callsTo('bar', anything, anything)).thenReturn('E'); |
| 53 m.when(callsTo('foobar')).thenReturn('F'); | 55 m.when(callsTo('foobar')).thenReturn('F'); |
| 54 | 56 |
| 55 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' | 57 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' |
| 56 '${m.bar(1,1)}${m.foo(1,2)}'; | 58 '${m.bar(1,1)}${m.foo(1,2)}'; |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 test('Spys', () { | 728 test('Spys', () { |
| 727 var real = new Foo(); | 729 var real = new Foo(); |
| 728 var spy = new Mock.spy(real); | 730 var spy = new Mock.spy(real); |
| 729 var sum = spy.sum(1, 2, 3); | 731 var sum = spy.sum(1, 2, 3); |
| 730 expect(sum, 6); | 732 expect(sum, 6); |
| 731 expect(() => spy.total(1, 2, 3), throwsNoSuchMethodError); | 733 expect(() => spy.total(1, 2, 3), throwsNoSuchMethodError); |
| 732 spy.getLogs(callsTo('sum')).verify(happenedExactly(1)); | 734 spy.getLogs(callsTo('sum')).verify(happenedExactly(1)); |
| 733 spy.getLogs(callsTo('total')).verify(happenedExactly(1)); | 735 spy.getLogs(callsTo('total')).verify(happenedExactly(1)); |
| 734 }); | 736 }); |
| 735 } | 737 } |
| 736 | |
| OLD | NEW |