[英]Jasmine can't run specs inside Javascript prototype methods
我剛剛開始使用Jasmine對JavaScript代碼進行單元測試,並且發現如果我通過對象的原型方法內部的describe()
或it()
定義套件或規格,則無法識別作為SpecRunner html頁面上的規范。
有沒有辦法解決? 因為我所有的代碼都是通過原型對象/方法編寫的,並且如果我無法在其中進行測試,那么我什么也無法測試,而Jasmine對我來說幾乎沒有用。 另外,如果沒有,那么是否有可以在原型方法/等中運行的良好的JS單元測試框架?
您可以使用茉莉花輕松測試在對象原型上定義的方法。 考慮一個基本的Person對象。
function Person(name)
{
this._name = name;
}
Person.prototype.getName = function()
{
return this._name;
};
要測試getName函數,請使用以下命令。
describe('Person test suite', function()
{
it('tests getName', function()
{
var p = new Person('Joe');
expect(p.getName()).toBe('Joe');
});
});
您還可以監視對象原型上的方法。 例如。
spyOn(Person.prototype, 'getName');
// Call the method.
expect(Person.prototype.getName).toHaveBeenCalled();
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.