chai中的断言失败不会将测试报告为失败。
我尝试使用assert而不是期望。我试图通过丢失期望值中的字符来导致测试失败。
const axios = require('axios');
var assert = require('assert');
var expect = require('chai').expect;
describe('Tests', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
it('should return 1 when index is 2', function () {
assert.equal([1, 2, 3].indexOf(3), 2)
});
});
describe('#http-get', function () {
it('should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg', function () {
axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
// assert.equal(response.data.url, 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg');
expect(response.data.url).to.equal('https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp');
})
.catch(error => {
console.log(error);
});
});
});
});
我希望输出状态为2合格而1状态失败,但是我看到以下输出,其中第三个断言被标记为合格,但是打印出断言失败。
Tests
#indexOf()
✓ should return -1 when the value is not present
✓ should return 1 when index is 2
#http-get
✓ should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg
3 passing (34ms)
{ AssertionError: expected 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg' to equal 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp'
at axios.get.then.response (/Users/adityai/nodejs-workspace/axios-sample/test/axios-sample-test.js:20:50)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
message: 'expected \'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg\' to equal \'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp\'',
showDiff: true,
actual: 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg',
expected: 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp' }
答案 0 :(得分:1)
无论您使用的是expect
还是assert
,当断言失败时,chai都会引发错误。您应该不处理错误,因为Mocha会根据错误来确定测试用例是否应该失败。
此外,如果您的测试用例是异步的,请记住在异步任务完成时返回promise或调用done
回调。
describe('#http-get', function () {
it('should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg', function () {
return axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
expect(response.data.url).to.equal('https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp')
})
// .catch(error => {
// console.log(error);
// })
})
})