Javascript 试图用Jasmine测试jQuery
我已经用JavaScript创建了一个非常简单的账单拆分应用程序,并且正在使用Jasmine进行测试 我正在尝试测试这个Javascript 试图用Jasmine测试jQuery,javascript,jquery,jasmine,Javascript,Jquery,Jasmine,我已经用JavaScript创建了一个非常简单的账单拆分应用程序,并且正在使用Jasmine进行测试 我正在尝试测试这个计算函数: function Bill_Splitter(){ this.amount = 0; this.cost = 0; this.tip = 0; this.diners = 0; }; Bill_Splitter.prototype.calculate = function(){
计算函数:
function Bill_Splitter(){
this.amount = 0;
this.cost = 0;
this.tip = 0;
this.diners = 0;
};
Bill_Splitter.prototype.calculate = function(){
this.cost = parseInt(document.getElementById("cost").value);
this.tip = parseInt(document.getElementById("tip").value);
this.diners = parseInt(document.getElementById("diners").value);
var amount = (cost + tip) / diners
return this.amount += amount
document.getElementById("amount").innerHTML = amount
}
但是,我无法确定如何测试数值(成本、小费和价值),这些数值是用户输入的HTML表单中的值
到目前为止,我的茉莉花测试是:
describe('calculate', function() {
const form = document.createElement('form');
form.innerHTML= `<input type="text" id="cost" value=2 />
<input type="text" id="tip" value=2 />
<input type="text" id="diners" value =2 />
<span id="amount"></span>
`;
document.body.appendChild(form)
splitter.calculate()
expect(splitter.amount).toEqual(30)
});
});
描述('计算',函数(){
const form=document.createElement('form');
form.innerHTML=`
`;
document.body.appendChild(表单)
splitter.calculate()
期望值(拆分金额)。toEqual(30)
});
});
有人能帮忙吗?谢谢!:) 您可以像在函数构造函数中使用this.amount那样创建属性
function Bill_Splitter(){
this.amount = 0;
this.cost = 0;
this.diners = 0;
this.tip = 0;
};
如果您的测试找不到这些html元素,您可以将它们添加到测试中:
it('calculate', function(){
const form = document.createElement('form');
form.innerHTML= `<input type="text" id="cost" value="2" />
<input type="text" id="tip" value="2" />
<input type="text" id="dinners" value ="2" />
<span id="amount"></span>
`;
document.body.appendChild(form)
splitter.calculate()
expect(splitter.amount).toEqual(30)
expect(splitter.tip).toEqual(###)
})
谢谢-但是我仍然收到相同的错误,即:无法读取null的属性“value”
此错误消息是因为测试找不到de DOM elementdocument。getElementById(“cost”)//为null我已使用更新的代码更新了原始帖子。DOM元素是一个表单,用户可以为其输入成本、小费和就餐人数。现在更新了我的示例,介绍了如何添加html
describe('calculate', function() {
it('calculates the amount due', function(){
splitter.calculate()
expect(splitter.amount).toEqual(30)
expect(splitter.tip).toEqual(###)
});
});
it('calculate', function(){
const form = document.createElement('form');
form.innerHTML= `<input type="text" id="cost" value="2" />
<input type="text" id="tip" value="2" />
<input type="text" id="dinners" value ="2" />
<span id="amount"></span>
`;
document.body.appendChild(form)
splitter.calculate()
expect(splitter.amount).toEqual(30)
expect(splitter.tip).toEqual(###)
})
function Bill_Splitter(){
this.amount = 0;
this.cost = 0;
this.tip = 0;
this.diners = 0;
};
Bill_Splitter.prototype.calculate = function(){
this.cost = parseInt(document.getElementById("cost").value);
this.tip = parseInt(document.getElementById("tip").value);
this.diners = parseInt(document.getElementById("diners").value);
this.amount += (this.cost + this.tip) / this.diners;
document.getElementById("amount").innerHTML = this.amount
}