dart-unittest

Dart How to mock a procedure

独自空忆成欢 提交于 2019-12-05 12:28:55
How do I go about mocking a procedure (as apposed to a function see here ) For example, given the following typedef and procedure, typedef int Adder(int a, int b); int useAdder(Adder adder) { return adder(1, 2); } How could you write a mock that would allow you to test that the userAdder procedure called you mocked function? This was my attempt, but it fails with the message that test failed: Caught The null object does not have a method 'call'. class MyMock extends Mock { MyMock(){ when(callsTo('call')).alwaysCall(this.foo); } int foo(int a, int b) => a+b; } void main() { test("bb", () { var

How to wait for an asynchronous setup in a unit test, in Dart?

一笑奈何 提交于 2019-12-04 19:33:45
问题 My unit tests require a setup that needs to run asynchronously. That is, I need to wait for the setup to finish before the tests are run, but the setup deals with Futures. 回答1: With Dart M3, the setUp function can optionally return a Future . If setUp returns a Future, the unittest framework will wait for the Future to complete before running the individual test methods. Here is an example: group(('database') { var db = createDb(); setUp(() { return openDatabase() .then((db) =>

How to mock server response - client on server side

一个人想着一个人 提交于 2019-12-04 08:12:20
I'm trying dart and I'm writing a client on the server side : new HttpClient().post(InternetAddress.LOOPBACK_IP_V4.host, 7474, '/path').then((HttpClientRequest request) { request.headers.contentType = ContentType.JSON; request.headers.add(HttpHeaders.ACCEPT, ContentType.JSON); request.write(JSON.encode(jsonData)); return request.close(); }).then((HttpClientResponse response) { response.transform(UTF8.decoder).listen((contents) { _logger(contents); // stuff }); }); and I would like to mock the server response. What is the best solution ? Create a server in my test class that will return the

WebStorm DartUnit with test api, run/debug error

最后都变了- 提交于 2019-12-02 12:24:23
问题 I'm using WebStorm IDE and test api which is named unittest before. The test works fine using the command: pub run test:test test/sample.dart /test/sample.dart library testproject.sample; import 'package:test/test.dart'; void main() { test('Testing', () { expect(true, isTrue); }); } but when i tried to run/debug using WebStorm DartUnit the test, the console shows this error: Unhandled exception: Uncaught Error: Load Error: FileSystemException: Cannot open file, path = 'C:\Users\dummy\Desktop

WebStorm DartUnit with test api, run/debug error

北城以北 提交于 2019-12-02 03:02:36
I'm using WebStorm IDE and test api which is named unittest before. The test works fine using the command: pub run test:test test/sample.dart /test/sample.dart library testproject.sample; import 'package:test/test.dart'; void main() { test('Testing', () { expect(true, isTrue); }); } but when i tried to run/debug using WebStorm DartUnit the test, the console shows this error: Unhandled exception: Uncaught Error: Load Error: FileSystemException: Cannot open file, path = 'C:\Users\dummy\Desktop\testproject\packages\unittest\unittest.dart' (OS Error: The system cannot find the path specified. ,