Rspec controller does not get render response

I have tried writing specs for the controller (lambda handler) and referenced the jets source code to see how it is done and found that the test for the controller (lambda handler) is more like a unit test and not similar to a rails controller test. In rails you can test the responses of the render method but in jets you cannot as its not really running a webserver during tests.

When I inspect the contents of the response of the controller I get something like the below which is the return value of the handler method, not the json response of the render method which is what I do want to test.
[“200”, {“Access-Control-Allow-Origin”=>"*", “Access-Control-Allow-Credentials”=>“true”, “Content-Type”=>“application/json”, “x-jets-base64”=>“no”}, #<StringIO:0x00007fdee79cad38>]

Anyone know how I could test the render method of the handler?

Can define your own helper methods in spec/spec_helper.rb currently. Don’t have a lot of spec helpers built into Jets yet. Others have started adding some pretty nice PRs to help though. So hopefully others can help in this area also :+1:

Hi Tung,

Yes I realise that the spec helpers are quite undeveloped. I am looking into how we can test the handlers better by referencing rails code and trying to understand how it is done there. If I can port it over, I will include a pull request for you to review.

1 Like

Hello @zeetao,

Have my spec_helper.rb:
This is a little hacky, but I’m planing introduce a cleaner version to Jets directly. Here is how you can use it:

    it "add application" do
      expect do
        post('/catalogs/:catalog_id/apps', catalog_id:, name: 1)

        body = JSON.parse(response.body, symbolize_names: true)
        expect(body.keys).to contain_exactly(*required_attributes) change { App.count }.by(1)
1 Like

Ooh! I will try this. Thanks Genail.

I have tried this solution and it works quite well to get the results of the render method. Thanks very much for this Genail.
I feel this should be included in the jets codebase. Very useful. Thanks again