Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Full example of how to test with the <code>completion</code> matcher is as follows.</p> <pre class="lang-java prettyprint-override"><code>import 'package:unittest/unittest.dart'; class Compute { Future&lt;Map&gt; sumIt(List&lt;int&gt; data) { Completer completer = new Completer(); int sum = 0; data.forEach((i) =&gt; sum += i); completer.complete({"value" : sum}); return completer.future; } } void main() { test("testing a future", () { Compute compute = new Compute(); Future&lt;Map&gt; future = compute.sumIt([1, 2, 3]); expect(future, completion(equals({"value" : 6}))); }); } </code></pre> <p>The unit test runner might not complete before this code completes. So it would seem that the unit test executed correctly. With <code>Future</code>s that might take longer periods of time to complete the proper way is to utilize <code>completion</code> matcher available in unittest package. </p> <pre class="lang-java prettyprint-override"><code>/** * Matches a [Future] that completes succesfully with a value that matches * [matcher]. Note that this creates an asynchronous expectation. The call to * `expect()` that includes this will return immediately and execution will * continue. Later, when the future completes, the actual expectation will run. * * To test that a Future completes with an exception, you can use [throws] and * [throwsA]. */ Matcher completion(matcher) =&gt; new _Completes(wrapMatcher(matcher)); </code></pre> <p>One would be tempted to do the following which would be incorrect way of unit testing a returned Future in dart. WARNING: below is an incorrect way to test Futures. </p> <pre class="lang-java prettyprint-override"><code>import 'package:unittest/unittest.dart'; class Compute { Future&lt;Map&gt; sumIt(List&lt;int&gt; data) { Completer completer = new Completer(); int sum = 0; data.forEach((i) =&gt; sum+=i); completer.complete({"value":sum}); return completer.future; } } void main() { test("testing a future", () { Compute compute = new Compute(); compute.sumIt([1, 2, 3]).then((Map m) { Expect.equals(true, m.containsKey("value")); Expect.equals(6, m["value"]); }); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload