Angular jasmine spyon实例

211 阅读1分钟

Jasmine spyon是一个双重功能,用于测试Javascript和typescript中的方法和对象。

它基本上允许你调用方法或函数并返回结果。

语法

spyOn(object,"function").withArgs(arguments).and.returnValue(value);

从一个类的构造函数中调用一个方法

myclass = function() { this.mymethod(); };

myclass.prototype.mymethod = function() { console.log("mymethod") }

describe("myclass构造函数", function() { it("应调用其原型的mymethod", function() { spyOn(myclass.prototype, 'mymethod'); //.andCallThrough(); var myc = new myclass(); expect(myclass.prototype.mymethod).toHaveBeenCalled(); });