Search code examples
javascriptjetbrains-ideriderweb-api-testingintellij-http-client

Why are the tests of the response not failing in Rider http-client?


A webservice returns this json

{
  "title": "Test how to filter the response",
  "features": [
    { "feature": "AB1", "price": "1.00"},
    { "feature": "AB1", "price": "1.23"},
    { "feature": "CD2", "price": "2.00"},
    { "feature": "EF3", "price": "3.00"},
    { "feature": "GH4", "price": "4.00"}
  ]
}

With or in my case the -implementation of http-client the following code should

  • test that AB1 was only 1 time present (expected = 1, actual = 2)
  • test that the price of AB1 is 0.00 (expected) and not something else (actual = 1.00 and 1.23)
> {%
     
    // this should fail since 2 AB1-features were sent
    client.test("feature AB1 was sent 1 time", function() {
        var features = response.body.features;         
        const result = features.filter(obj => obj.feature === "AB1");
        result.forEach(r => client.log(JSON.stringify(r)));
        client.assert(result.length, "1"); 
    });
    
    // should fail since the price is not 0
    client.test("feature AB1 price is 0.00", function() {
        var features = response.body.features;              
        const result = features.filter(obj => obj.feature === "AB1");
        client.assert(result[0].price, "0.00");  
        client.log("price of " + result[0].feature + " = " + result[0].price); 
        
    });     
%}

But the above code used in has not the result i expected

JetBrains_Rider_http-client

Question

  • Why are the tests not failing?
  • Where is the flaw / mistake in my code?

Solution

  • The cause was a wrong use of the http-client API: client.assert(assertion, [message]) where the assertion must be a boolean expression. The message is optional.

    // must contain an expression that returns a boolean
    client.assert(result.length == "1")