I'm not quite sure I understand what's going on when I try testing post HttpRequest. Here's the code of my class that does the job:
import 'dart:html';
class HttpReportAdapter {
var logmaster;
int log_level = 2;
String url;
HttpReportAdapter(this.url) {}
post(r, level) {
var data = {
'message' : r,
'log_level' : log_level.toString(),
};
if(this.logmaster != null)
data['log_level_string'] = this.logmaster.log_level_as_string(level);
return HttpRequest.postFormData(this.url, data);
}
}
An here's the test code:
import '../lib/report_adapters/http_report_adapter.dart';
import "package:test/test.dart";
void main() {
var adapter;
setUp(() {
adapter = new HttpReportAdapter("http://localhost:4567/errors");
});
test("sends an ajax request and acknowledges a 200 response from the server", () {
adapter.post("message", 2).then((_) => print("!!!!!!!!!"));
});
}
For now, as you can see, I'm not even trying to test anything, just output something. While running this, the request does indeed to to http://localhost:4567/errors
and I can see it in my server's logs.
However, the !!!!!!!!
isn't printed.
Additionally, the test fails with:
This test failed after it had already completed. Make sure to use
[expectAsync] or the [completes] matcher when testing async code.
However, if I force my server to sleep for 1 second before sending a response, the test passes without an error.
I would appreciate if someone would help me make some sense of it all, and, consequently, write the correct test.
You need to return the Future
so the test can wait for it to complete
return adapter.post("message", 2).then((_) => print("!!!!!!!!!"));
Otherwise the test completes before the response from the server arrives.