0

I'm trying to write a simple Test for my Controller. I use this documentation from Sails.js

The UserController.test.js:

var request = require('supertest');

describe('UserController', function () {

    describe('#login()', function () {
        it('should redirect to / indexpage', function (done) {
            request(sails.hooks.http.app)
                .post('/login')
                .send({name: 'Stifflers', password: 'Mom'})
                .expect(302)
                .expect('Location', '/', done);
        });
    });

});

The relevant code from AuthController.js:

...
   // is authenticated
      res.writeHead(302, {
        'Location': "/"
       });

       res.end(); 
...

I run the test with npm test and get this error:

Error: expected 302 "Found", got 200 "OK"

When I change the .expect(302) in my test to .expect(200) I get the next error:

Error: expected "Location" header field

I have tried to do it the same like in the documentation, why doesn't it work?

Suisse
  • 3,467
  • 5
  • 36
  • 59
  • Why don't you use ```res.redirect(302, '/')``` ? – Bonanza Jun 27 '16 at 10:34
  • @Bonanza That doesn't matter, we get the same effect. 1: http://expressjs.com/en/api.html#res.redirect 2: https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers – Suisse Jun 27 '16 at 14:33
  • Have you confirmed that it's actually executing the correct route? It doesn't seem to be. – Kevin B Jun 27 '16 at 15:28
  • I have in config/routes.js: 'post /login': 'AuthController.login', And the above redirect is in AuthController.login() function. – Suisse Jun 27 '16 at 16:16
  • omg, my fault... omg... my test password was not correct. It was everything just about that. But how could I find this dumb error earlier??? omg – Suisse Jun 27 '16 at 16:17
  • actually I sent wrong post variables. – Suisse Jun 27 '16 at 16:24

1 Answers1

0

call me dumb... the error was caused by the wrong post variables which I were sent.

            .send({email: 'Test@test.ch', password: 'test'})

I had to send email not name! Everytime I tested it with postman with "email", and in my code I had "name".

I saw there are a lot of similar questions on SO and elsewhere; Maybe my fault can help them to check their own code better.

Suisse
  • 3,467
  • 5
  • 36
  • 59