dart-unittest

UnitTest example for asynchronous code

会有一股神秘感。 提交于 2019-12-24 02:18:42
问题 After reading the Unit Testing with Dart somehow I'm still can not understand how to use it with Future s. For example: void main() { group('database group',(){ setUp( () { // Setup }); tearDown((){ // TearDown }); test('open connection to local database', (){ DatabaseBase database = null; expect(database = new MongoDatabase("127.0.0.8", "simplechat-db"), isNotNull); database.AddMessage(null).then( (e) { expectAsync1(e) { // All ok } }, onError: (err) { expectAsync1(bb) { fail('error !'); } }

Mocking with Dart

白昼怎懂夜的黑 提交于 2019-12-23 03:42:17
问题 I've been trying to get my head around the mocking library in dart, but it seems I've still not got it. In my library, I have an HTTP request to an external resource, which I would like to mock as to not rely on the external resource all the time. The main class in my library looks like this: SampleClass(String arg1, String arg2, [http.Client httpClient = null]) { this._arg1 = arg1; this._arg2 = arg2; _httpClient = (httpClient == null) ? http.Request : httpClient; } So I have prepared my

How to mock server response - client on server side

二次信任 提交于 2019-12-21 17:29:04
问题 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

Dart Event Queue: How to debug event queue?

只愿长相守 提交于 2019-12-12 14:55:14
问题 I'm writing some integration tests which utilize an HttpServer , a bunch of Directory().watch() 'ers and possibly some other future/stream listening code. I'm using the following test configuration: class LaserServerTestConfiguration extends SimpleConfiguration { LaserServer _server; LaserServerTestConfiguration(this._server); void onDone(bool success) { super.onDone(success); _server.stop(); } } And my tests look like this: main() { var conf = new LaserConfiguration.fromPath('test/test

Mocking HTTP response with Dart

丶灬走出姿态 提交于 2019-12-12 14:34:04
问题 I have been working on a new API wrapper and don't want to be calling the API every time my unit tests run. So as described here, I'm mocking it. I initially thought there was something wrong with the way I'm mocking it, but it seems the problem is elsewhere. What I'm trying to accomplish is very simple. When my unit test run, I would like to return a value as if I had gone out to get the information from the external API I'm integrating with. I initialise my class with http.Client as an

Why the async test passed, but there are some error messages displayed?

谁都会走 提交于 2019-12-11 09:03:05
问题 Dart test code: _doSomething2(callback(int x, int y)) { callback(1, 2); } test('async test, check a function with 2 parameters', () { new Timer(new Duration(milliseconds:100), _doSomething2(expectAsync2((x, y) { expect(x, equals(1)); expect(y, equals(2)); }))); }); When I run it in Intellij-IDEA as "unittest", it passed, but there is some error message shown: Testing started at PM11:08 ... Unhandled exception: The null object does not have a method 'call'. NoSuchMethodError : method not found

Dart Mocking a Function

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:16:06
问题 How do I test that a mocked function was called? I found this example on Mocking with Dart - How to test that a function passed as a parameter was called? and tried to extend it to check if the function was called. library test2; import "package:unittest/unittest.dart"; import "package:mock/mock.dart"; class MockFunction extends Mock { call(int a, int b) => a + b; } void main() { test("aa", () { var mockf = new MockFunction(); expect(mockf(1, 2), 3); mockf.getLogs(callsTo(1, 2)).verify

How to debug browser tests with the new test package

旧城冷巷雨未停 提交于 2019-12-10 20:18:05
问题 browser tests need to be run from command line like pub run test -pdartium . Is there a way to debug such tests. 回答1: pub run test -pdartium --pause-after-load starts the test only after I click a "play" button on the test page. This gives me time to open up Dev Tools in Dartium and set breakpoints. I can also open up the Observatory and do stuff there. 回答2: The Dart team is working hard to make tests with the new test package debuggable. Until the related issues are fixed you can use this

Dart How to mock a procedure

丶灬走出姿态 提交于 2019-12-10 08:23:13
问题 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

How to test a private function, in Dart?

我的未来我决定 提交于 2019-12-08 14:44:26
问题 Say I defined a private function in a dart file hello.dart : _hello() { return "world"; } I want to test it in another file mytest.dart : library mytest; import 'dart:unittest/unittest.dart'; main() { test('test private functions', () { expect(_hello(), equals("world")); } } But unfortunately, the test code can't be compiled. But I do need to test that private _hello function. Is there any solution? 回答1: While I agree that private methods/classes shouldn't be part of your tests, the meta