HttpClient&单元测试

时间:2014-06-06 02:21:07

标签: unit-testing dart dart-async

一般是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));
        }));
    });
  });
}

UPDATE:决定放弃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));
      }));
    });
  });
}

2 个答案:

答案 0 :(得分: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
    }));
});

答案 1 :(得分:0)

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