gpt4 book ai didi

unit-testing - HttpClient & 单元测试

转载 作者:行者123 更新时间:2023-12-04 05:01:55 24 4
gpt4 key购买 nike

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));
}));
});
});
}

最佳答案

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

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

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

我认为测试应该做同样的事情。据我所知,有一个开放的错误,对于测试本身,当异步操作开始时返回 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
}));
});

关于unit-testing - HttpClient & 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24073187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com