3

Dart、单元测试和一般异步编程的新手。和 StackOverflow,就此而言。尝试围绕shelf_route 构建一个RESTful 服务器并使用HttpClient 对其进行测试。

我的测试有效,但它们不会自行退出。相反,我必须手动终止该过程。我尝试了几种我在网上找到的方法,但没有运气。谁能帮我一把?这是我正在使用的:

import 'dart:io';
import 'package:unittest/unittest.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:toner_track/handlers.dart';
import 'package:toner_track/config.dart';

void main() {
  var server;

  group('Server tests', () {
    var server;

    setUp(() {
      var handler = const shelf.Pipeline().addHandler(router);
      io.serve(handler, HOST, PORT).then((s) {
          server = s;
      });
    });

    tearDown(() {
      server.close();
    });

    test('Invalid URL returns 404', () {
      new HttpClient().getUrl(Uri.parse('http://$HOST:$PORT'))
        .then((HttpClientRequest request) => request.close())
        .then(expectAsync((HttpClientResponse response) {
          expect(response.statusCode, equals(404));
        }));
    });

    test('GET /printers returns 200', () {
      new HttpClient().getUrl(Uri.parse('http://$HOST:$PORT$rootPrintersPath'))
      .then((HttpClientRequest request) => request.close())
      .then(expectAsync((HttpClientResponse response) {
        expect(response.statusCode, equals(200));
      }));
    });

    test('POST /printers returns 201', () {
      new HttpClient().postUrl(Uri.parse('http://$HOST:$PORT$rootPrintersPath'))
        .then((HttpClientRequest request) {
          request.write('{"foo": 42}');
          return request.close();
        })
        .then(expectAsync((HttpClientResponse response) {
          expect(response.statusCode, equals(201));
        }));
    });
  });
}

更新:决定放弃 HttpClient 以支持该http包。更容易使用,我的测试现在自行终止。显然,我在第一次复飞时没有正确处理某些事情。这是上面的代码现在的样子:

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:unittest/unittest.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:toner_track/handlers.dart';
import 'package:toner_track/config.dart';

void main() {
  group('Server tests', () {
    var server;

    setUp(() {
      var handler = const shelf.Pipeline().addHandler(router);
      return io.serve(handler, HOST, PORT).then((s) {
        server = s;
      });
    });

    tearDown(() {
      server.close();
    });

    test('Invalid URL returns 404', () {
      http.get('http://$HOST:$PORT').then(expectAsync((response) {
        expect(response.statusCode, equals(404));
      }));
    });

    test('GET /printers returns 200', () {
      http.get('http://$HOST:$PORT$rootPrintersPath').then(expectAsync((response) {
        expect(response.statusCode, equals(200));
      }));
    });

    test('POST /printers returns 201', () {
      http.post('http://$HOST:$PORT$rootPrintersPath', body: {"foo": "42"}).then(expectAsync((response) {
          expect(response.statusCode, equals(201));
      }));
    });
  });
}
4

2 回答 2

3

看起来这不是您的问题的原因,但是如果您在其中启动异步操作,则setUp应该返回 Future。

return io.serve(handler, HOST, PORT).then((s) {
      server = s;

单元测试框架在开始运行测试之前等待未来返回。

我认为测试应该做同样的事情。据我记得有一个开放的错误,对于测试本身来说,在异步操作开始时返回 Future 就足够了,但它在这里不像setUp. 这是前一段时间,所以我认为这是固定的。

test('Invalid URL returns 404', () {
  return new HttpClient().getUrl(Uri.parse('http://$HOST:$PORT'))
    .then((HttpClientRequest request) => request.close())
    .then(expectAsync((HttpClientResponse response) {
      expect(response.statusCode, equals(404));
    }));
});

如果这不起作用

test('POST / ...


test('Invalid URL returns 404', () {
  var cb = new expectAsync((){});

  new HttpClient().getUrl(Uri.parse('http://$HOST:$PORT'))
    .then((HttpClientRequest request) => request.close())
    .then(expectAsync((HttpClientResponse response) {
      expect(response.statusCode, equals(404));
      cb(); // <== to notify the unit test framework that the async operation is finished
    }));
});
于 2014-06-06T04:08:39.787 回答
0

在 main 函数的开头添加以下内容: useGoogle3VMConfiguration();

于 2014-11-14T07:21:02.343 回答