柴断言总是好的

时间:2018-03-31 12:18:08

标签: node.js mocha chai

我正在尝试使用mocha和chai断言测试node.js api。

我的问题是我总是得到一个结果'。

测试:

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-testing chai@chai.com chai-testing

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 个答案:

答案 0 :(得分:0)

有两个断言而不是这个:

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();
     });
    });
   });