redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Help: Using redstone_mapper with redstone_mocks, having issue with null body #75

Closed austincummings closed 9 years ago

austincummings commented 9 years ago

Hi @luizmineo,

I'm trying to implement a RESTful web service. I'm using your framework for the routing, mapping, and unit testing.

So I have this Query type that I declare 4 fields in, id, owner, sqlSource, and markedForDeletion. I also have a group to define the REST resource.

@web.Group("/queries")
class QueriesCollection {
    @web.DefaultRoute(methods: const [web.GET])
    List<Query> getAllQueries() {
        // Retrieve every query in the collection.
    }

    @web.DefaultRoute(methods: const [web.POST])
    Future<shelf.Response> createNewQuery(@Decode() Query newQuery) async {
        print(newQuery);

        // Retrieve every query in the collection.
        final String queriesFSRoot = "./queries";
        File queryFile = new File("$queriesFSRoot/${newQuery.id}.wql");

        if (!await queryFile.exists()) {
            // Does not exist, we can create it if the user has permission.
            // TODO XXX: Check if the user has permissions.
            queryFile.writeAsString(encode(newQuery));

            var response = new shelf.Response(HttpStatus.OK, headers: {
                "Location": "/queries/${newQuery.id}"
            });
            return response;
        } else {
            // Query already exists, return 409 CONFLICT.
            var response = new shelf.Response(HttpStatus.CONFLICT);
            return response;
        }
    }

}

As you can see, I want the Route to automagically decode the Query in the body. I'm testing it using this unit test...

void main() {
    setUp(() {
        print("Setting up");
        SqlRepository.sqlDirectory = "./lib/server/sql/";
        SqlRepository.loadAll();
        config.load("./config.json");

        web.setUp();
        bootstrapMapper();
    });

    test("create new query", () async {
        Query query = new Query()
            ..id = 1
            ..owner = "acummings"
            ..sqlSource = "test"
            ..markedForDeletion = false;

        var req = new MockRequest("/queries",
            method: web.POST,
            body: encode(query));

        return web.dispatch(req).then((resp) {
            print(resp.mockContent);
            expect(resp.statusCode, equals(200));
            expect(resp.mockContent, equals("ok"));
        });
    });
}

My problem is that newQuery is always null in the Route when I run it through with the mock framework.

As far as I know I'm doing things right, but obviously I'm not and was hoping you could help me point out my error.

Thanks!

austincummings commented 9 years ago

I believe I solved it already. I was calling bootstrapMapper() rather than web.addPlugin(getMapperPlugin()) in setUp()