Утверждение всегда хорошо

Я пытаюсь протестировать api node.js с утверждением mocha и chai.

Моя проблема в том, что я всегда получаю "пройденный результат".

Тест:

describe('TESTING /register', () => {

it('Should create a new account for chai-testing:chai-testing', () => {
    let user = {
        pseudo: "chai-testing",
        mail: "chai@chai.com",
        password: "chai-testing"
    };

    chai.request(server)
    .post('/register')
    .send(user)
    .end((err, resp) => {
        console.log(resp.body);
        resp.body.should.have.property('success').eql(true);
        done();
    });
});
});

И вывод консоли:

  TESTING /register
✓ Should create a new account for chai-testing:chai-testing

Чай-тестирование chai@chai.com Чай-тестирование

1 passing (51ms)

{ favoris: [],
  _id: 5abf6b5502c0f910439fec32,
  pseudo: 'chai-testing',
  mail: 'chai@chai.com',
  password: '$2a$10$BPzQfp3wiDxU3mwgeXkG8Oh.B1ET8wTt5kg12oBwQ0obUxAyZQdLu',
  admin: false,
  __v: 0 }
POST /register 200 281.742 ms - 51
{ **success: false**, message: 'pseudo already taken' }

Что я сделал не так в моем тесте?

1 ответ

Вместо этого поставьте два утверждения:

describe('TESTING /register', () => {

it('Should create a new account for chai-testing:chai-testing', () => {
   let user = {
    pseudo: "chai-testing",
    mail: "chai@chai.com",
    password: "chai-testing"
  };

chai.request(server)
.post('/register')
.send(user)
.end((err, resp) => {
    console.log(resp.body);
    // if the resp has an attribute success then this
    // assertion is true ( .eql(true) )  
    // that's why you get the test always passing 
    resp.body.should.have.property('success');

    resp.body.success.should.eql(true);
    done();
     });
    });
   }); 
Другие вопросы по тегам