mercredi 5 avril 2017

Type.Error app.address is not a function

I'm trying test my node app ,I am able to get post,get and put to work but I can't get delete to work I keep on getting the following error "Type.Error app.address is not a function"

This is my test class :

var supertest = require("supertest");
var should = require("should");

// This agent refers to PORT where program is runninng.
var server = supertest.agent("http://localhost:8000");

// UNIT test begin

describe("Contacts GET unit test",function(){
  // #1 should return contacts representation in json
  it("should return collection of JSON documents",function(done){

    // calling home page api
    server
    .get("/api/contacts/")
    .expect("Content-type",/json/)
    .expect(200) // THis is HTTP response
    .end(function(err,res){
      // HTTP status should be 200
      res.status.should.equal(200);
      done();
    });
  });

it("should add a new contact",function(done){

  // post to /api/contacts
  server.post('/api/contacts')
  .send({name:"Contact 99"})
  .expect("Content-type",/json/)
  .expect(201)
  .end(function(err,res){
    res.status.should.equal(201);
    done();
  });
});

it("should update contact",function(done){
// post to /api/contacts
// calling home page api

        server.put("/api/contacts/58e275d0aef2392e10eaba6e")
        .expect("Content-type",/json/)
        .expect(200) // THis is HTTP response
        .end(function(err,res){
            res.body._id.should.equal("58e275d0aef2392e10eaba6e");
            done();
         }
       );
       }
     );


it("should delete contact",function(done){
// post to /api/contacts
// calling home page api
const superserver = supertest(server);
server.get("/api/contacts")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
    const id = res.body[0]._id;
   server.delete("/api/contacts/"+id)
        .expect("Content-type",/json/)
        .expect(200) // THis is HTTP response
        .end(function(err,res){
            res.body._id.should.equal(id);
            res.body.should.have.property("name");
            done();
         }
       );
       }
     );
});
});

and this my main class with all the functionalities :

'use strict';

var _ = require('lodash');
var Contact = require('./contact.model');

// Get list of contacts
exports.index = function(req, res) {
          // Connect to the db
   Contact.find(function (err, contacts) {
    if(err) { return handleError(res, err); }
    return res.json(200, contacts);
  });

} ;

// Creates a new contact in datastore.
exports.create = function(req, res) {
  Contact.create(req.body, function(err, contact) {
    if(err) { return handleError(res, err); }
    return res.json(201, contact);
  });
};

// Updates an existing contact in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Contact.findById(req.params.id, function (err, contact) {
    if (err) { return handleError(res, err); }
    if(!contact) { return res.send(404); }
    var updated = _.merge(contact, req.body);
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, contact);
    });
  });
};

// delete an existing contact in datastore.
exports.delete = function(req, res) {
    Contact.findById(req.params.id, function (err, contact) {
    if(err) { return handleError(res, err); }
    if(!contact) { return res.send(404); }
    contact.remove(function(err) {
      if(err) { return handleError(res, err); }
      return res.send(201);
    });
  });
};

function handleError(res, err) {
  return res.send(500, err);
};

This what I get in the command line: enter image description here

Aucun commentaire:

Enregistrer un commentaire