earldouglas / swagger-test

Specification-driven REST API testing
MIT License
113 stars 19 forks source link

AssertionError: expected { Object (description, request, ...) } to deeply equal { Object (description, request, ...) } #9

Closed ujjwal08 closed 7 years ago

ujjwal08 commented 7 years ago

I have modified example spec (refer http://editor.swagger.io/#/ example- heroku-pets) and i am trying to test this spec using swagger-test. But i am stuck at one point,

AssertionError: expected { Object (description, request, ...) } to deeply equal { Object (description, request, ...) }

What i think the reason is, i am not correctly mentioning the uri for get/ in tests.js file. Actually the uri should be- http://petstore-api.herokuapp.com/pet/?limit=11 and what my code is taking is http://petstore-api.herokuapp.com/pet/ . My question is,How to pass input parameters(i.e. ?limit=11) in the uri in tests.js? And if i am wrong, then how to resolve this issue ?

Below is the code for my swagger.json file:

{ "swagger": "2.0", "info": { "version": "1.0.0", "title": "PetStore on Heroku", "description": "This example has a working backend hosted in Heroku\n\nYou can try all HTTP operation described in this Swagger spec.\n\nFind source code of this API here\n" }, "host": "petstore-api.herokuapp.com", "basePath": "/pet", "schemes": [ "http", "https" ], "consumes": [ "application/json", "text/xml" ], "produces": [ "application/json", "text/html" ], "paths": { "/": { "get": { "parameters": [ { "name": "limit", "in": "query", "description": "number of pets to return", "type": "integer", "default": 11, "minimum": 11, "maximum": 10000 } ], "responses": { "200": { "description": "List all pets", "schema": { "title": "Pets", "type": "array", "items": { "$ref": "#/definitions/Pet" } } } } } }, "/{petId}": { "get": { "parameters": [ { "name": "petId", "in": "path", "type": "string", "description": "ID of the pet", "required": true } ], "responses": { "200": { "description": "Sends the pet with pet Id" } }, "x-amples": [ { "description": "get /{petId}: returns 200 for 560eea43a62de703006d2b57", "request": { "params": { "petId": "560eea43a62de703006d2b57" } }, "response": { "status": 200, "headers": { "content-type": "application/json" } } }, { "request": { "params": { "petId": "560eea59a62de703006d2b58" } }, "response": { "status": 200, "headers": { "content-type": "application/json" } } } ] } } }, "definitions": { "Pet": { "type": "object", "properties": { "name": { "type": "string" }, "birthday": { "type": "integer", "format": "int32" } } } } }

And below is the code for my tests.js file:

_'use strict';

/ global describe, it, before, beforeEach, after, afterEach /

var fs = require('fs'); var swaggerTest = require('../lib/swagger-test'); var assert = require('chai').assert;

describe('test generation with inference', function () {

var testDir = __dirname; var buffer = fs.readFileSync(testDir + '/swagger.json'); var spec = JSON.parse(buffer);

var xamples = swaggerTest.parse(spec, { inferXamples: true });

it('should contain three test cases', function () { assert.equal(xamples.length, 3); }); var str= xamples[0].toString(); //console.log("str: "+str); //console.log("xamples: "+xamples);

it('should first test GET /', function () { assert.deepEqual(xamples[0], { "description": "get /", "request": { "method": "get", "uri": "petstore-api.herokuapp.com/pet/" }, "response": { "status": 200, "headers": { "content-type": "application/json" } } }); });

it ('should next test GET /560eea43a62de703006d2b57', function () { assert.deepEqual(xamples[1], { "description": "get /{petId}: returns 200 for 560eea43a62de703006d2b57", "request": { "params": { "petId": "560eea43a62de703006d2b57" }, "method": "get", "uri": "petstore-api.herokuapp.com/pet/560eea43a62de703006d2b57" }, "response": { "status": 200, "headers": { "content-type": "application/json" } } }); });

it ('should next test GET /560eea59a62de703006d2b58', function () { assert.deepEqual(xamples[2], { "description": "get /{petId}", "request": { "params": { "petId": "560eea59a62de703006d2b58" }, "method": "get", "uri": "petstore-api.herokuapp.com/pet/560eea59a62de703006d2b58" }, "response": { "status": 200, "headers": { "content-type": "application/json" } } }); }); });_

And here is what my console is saying:

selection_010

How to resolve this error? And how to pass input parameters like this one "?limit=11".

earldouglas commented 7 years ago

The error message in your screenshot is telling you that the response included a content-type header, but that it was unexpected because it is not part of your Swagger specification.

Since you're using test inference, swagger-test needs a little help with details like this. Try manually adding a headers field to the response field of xamples[0]:

  it('should first test GET /', function () {
    xamples[0].response.headers = {
      'content-type': 'application/json'
    };
    assert.deepEqual(xamples[0], {
ujjwal08 commented 7 years ago

@earldouglas .. I am wondering how i missed to see that my spec response doesn't include content-type header. Thanks, for pointing that out! Issue resolved (y)