Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Unit test

luizmineo edited this page May 14, 2014 · 12 revisions

Basically, to create a test, you just need to:

  • Call setUp() to load your handlers
  • Create a MockRequest
  • Call dispatch() to send your request
  • Inspect the returned response
  • Call tearDown() to unload your handlers

Example:

library services;

import 'package:redstone/server.dart' as app;

@app.Route("/user/:username")
helloUser(String username) => "hello, $username";
import 'package:unittest/unittest.dart';

import 'package:redstone/server.dart' as app;
import 'package:redstone/mocks.dart';

import 'package:your_package_name/services.dart';

main() {

  //load handlers in 'services' library
  setUp(() => app.setUp([#services]));
  
  //remove all loaded handlers
  tearDown(() => app.tearDown());
  
  test("hello service", () {
    //create a mock request
    var req = new MockRequest("/user/luiz");
    //dispatch the request
    return app.dispatch(req).then((resp) {
      //verify the response
      expect(resp.statusCode, equals(200));
      expect(resp.mockContent, equals("hello, luiz"));
    });
  })
  
}