0
私は、約束の一部として返された関数をテストしています。私はchai-as-promisedを使用しています。約束によって返された関数をテストしてエラーをチェックします。
私はこの関数が機能することをテストできますが、エラーが正しくスローされることをテストできません。
私は約束に関連するコードがたくさん出て残して、テストしようとしている機能:
// function that we're trying to test
submitTest = (options) => {
// missingParam is defined elsewhere. It works - the error is thrown if screenshot not passed
if (missingParam(options.screenShot)) throw new Error('Missing parameter');
return {};
}
私のテスト:
describe('SpectreClient()', function() {
let client;
before(() => client = SpectreClient('foo', 'bar', testEndpoint));
// the client returns a function, submitTest(), as a part of a promise
/*
omitting tests related to the client
*/
describe('submitTest()', function() {
let screenShot;
before(() => {
screenShot = fs.createReadStream(path.join(__dirname, '/support/test-card.png'));
});
// this test works - it passes as expected
it('should return an object',() => {
const submitTest = client.then((response) => {
return response.submitTest({ screenShot });
});
return submitTest.should.eventually.to.be.a('object');
});
// this test does not work - the error is thrown before the test is evaluated
it('it throws an error if not passed a screenshot',() => {
const submitTest = client.then((response) => {
return response.submitTest({});
});
return submitTest.should.eventually.throw(Error, /Missing parameter/);
});
});
})
テストの出力を -
// console output
1 failing
1) SpectreClient() submitTest() it throws an error if not passed a screenshot:
Error: Missing parameter
エラーが発生したかどうかをテストするにはどうすればよいですか?私はそれがモカの問題か約束のものなのか、約束通りのものなのかは分かりません。大いに感謝します。