2
我試圖測試在shelf_rest上運行的DART REST應用程序。假設類似於shelf_rest
示例的設置,如何在不實際運行HTTP服務器的情況下測試配置的路由?使用dart的shelf_rest進行單元測試
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_rest/shelf_rest.dart';
void main() {
var myRouter = router()
..get('/accounts/{accountId}', (Request request) {
var account = new Account.build(accountId: getPathParameter(request, 'accountId'));
return new Response.ok(JSON.encode(account));
});
io.serve(myRouter.handler, 'localhost', 8080);
}
class Account {
final String accountId;
Account.build({this.accountId});
Account.fromJson(Map json) : this.accountId = json['accountId'];
Map toJson() => {'accountId': accountId};
}
class AccountResource {
@Get('{accountId}')
Account find(String accountId) => new Account.build(accountId: accountId);
}
沒有進入太多額外的邏輯,GET account
端點如何進行單元測試?一些基本的測試,我想運行將是:
GET /accounts/123
返回200GET /accounts/bogus
收益404