Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another possibility is to use expectAsync1 function. Working analogue for initial incorrect variant of test would be: </p> <pre class="lang-dart prettyprint-override"><code>void main() { test("testing a future", () { Compute compute = new Compute(); compute.sumIt([1, 2, 3]).then(expectAsync1((Map m) { Expect.equals(true, m.containsKey("value")); Expect.equals(6, m["value"]); })); }); } </code></pre> <p>One advantage in using expectAsync1 for async testing is its composability. Sometimes tests are naturally in need of several sequential async blocks of code. Sample test from mongo_db:</p> <pre class="lang-dart prettyprint-override"><code>testCursorGetMore(){ var res; Db db = new Db('${DefaultUri}mongo_dart-test'); DbCollection collection; int count = 0; Cursor cursor; db.open().chain(expectAsync1((c){ collection = db.collection('new_big_collection2'); collection.remove(); return db.getLastError(); })).chain(expectAsync1((_){ cursor = new Cursor(db,collection,where.limit(10)); return cursor.each((v){ count++; }); })).chain(expectAsync1((dummy){ expect(count,0); List toInsert = new List(); for (int n=0;n &lt; 1000; n++){ toInsert.add({"a":n}); } collection.insertAll(toInsert); return db.getLastError(); })).chain(expectAsync1((_){ cursor = new Cursor(db,collection,where.limit(10)); return cursor.each((v)=&gt;count++); })).then(expectAsync1((v){ expect(count,1000); expect(cursor.cursorId,0); expect(cursor.state,Cursor.CLOSED); collection.remove(); db.close(); })); } </code></pre> <p><strong>Update:</strong> </p> <p>Both Future and unittest API's were changed since question was initially asked. Now it is possible just return <code>Future</code> from test function and unittest properly executed it with all async guarded functionality. Combined with fact that <code>chain</code> and <code>then</code> methods of <code>Future</code> are now merged that provide nice syntax for tests with several sequential blocks of code. In current version of mongo_dart same test looks like: </p> <pre class="lang-dart prettyprint-override"><code>Future testCursorGetMore(){ var res; Db db = new Db('${DefaultUri}mongo_dart-test'); DbCollection collection; int count = 0; Cursor cursor; return db.open().then((c){ collection = db.collection('new_big_collection2'); collection.remove(); return db.getLastError(); }).then((_){ cursor = new Cursor(db,collection,where.limit(10)); return cursor.forEach((v){ count++; }); }).then((dummy){ expect(count,0); List toInsert = new List(); for (int n=0;n &lt; 1000; n++){ toInsert.add({"a":n}); } collection.insertAll(toInsert); return db.getLastError(); }).then((_){ cursor = new Cursor(db,collection,null); return cursor.forEach((v)=&gt;count++); }).then((v){ expect(count,1000); expect(cursor.cursorId,0); expect(cursor.state,State.CLOSED); collection.remove(); return db.close(); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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