我正在尝试编写一个测试,如果一个警报弹出,它就会通过,如果警报弹出失败,它将失败。
我在用摩卡和柴和西农。
下面是我想测试的函数:
function generateAlert(x){
if(x){
alert('X is true');
} else { return}
}我想做的事(psuedo):
describe('AlertView', function (){
it('should create an alert only when x is true', function(){
//check if alert is rendering and contains correct string
chai.assert.alertHappens(generateAlert(True), 'X is true');
//or at least check if alert happens at all
chai.assert.alertHappens(generateAlert(True), true);
}}
)}我是新来的摩卡柴,我不知道如何检查是否有警报或检查内容的警报。我翻阅了柴氏图书馆,却找不到任何能做到这一点的东西。是否有我错过的柴氏方法或其他检查警报的方法?
发布于 2016-07-18 19:36:54
我假设您正在测试的alert()是您正在测试的默认Window.alert函数。我建议您查看一下西农库,它允许您为现有函数创建间谍。因此,您可以检查函数是否被调用、调用了多少次以及使用了哪些参数。我会用间谍在您的代码中覆盖alert,只需要看到一个正确的参数被传递到警报中。下面是适用于我的代码:
'use strict';
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var alert; // We are going to overwrite default alert() function
function generateAlert(x) {
if (!x) {
return;
}
alert(x);
}
describe('AlertView', function() {
beforeEach(function() {
alert = sinon.spy();
});
it('should create an alert only when x is true', function() {
generateAlert(true);
expect(alert.calledOnce).to.be.true;
expect(alert.args[0][0]).to.equal(true);
});
it('should create an alert only when x is some string', function() {
generateAlert('X is true');
expect(alert.calledOnce).to.be.true;
expect(alert.args[0][0]).to.equal('X is true');
});
it('should not create an alert only when x is false', function() {
generateAlert();
expect(alert.callCount).to.equal(0);
});
});https://stackoverflow.com/questions/38444212
复制相似问题