调用同一文件中的测试函数
Testing function within same file is called
我有 2 个函数,一个函数调用另一个函数,另一个调用另一个函数 returns,但我无法让测试正常工作。
使用 expect(x).toHaveBeenCalledWith(someParams);
期望使用间谍,但我不知道如何监视同一文件中的函数...
Error: : Expected a spy, but got Function.
Usage: expect().toHaveBeenCalledWith(...arguments)
Example.ts
doSomething(word: string) {
if (word === 'hello') {
return this.somethingElse(1);
}
return;
}
somethingElse(num: number) {
var x = { "num": num };
return x;
}
Example.spec.ts
fake = {"num": "1"};
it('should call somethingElse', () => {
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
在您的 Example.spec.ts 中,只需添加 spyOn(component, 'somethingElse');
作为 it('should call somethingElse ...
测试的第一行:
fake = {"num": "1"};
it('should call somethingElse', () => {
// Add the line below.
spyOn(component, 'somethingElse');
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
expect 方法在 toHaveBeenCalledWith
之前使用时需要一个 Spy 作为参数(根据 Jasmine documentation)。
我有 2 个函数,一个函数调用另一个函数,另一个调用另一个函数 returns,但我无法让测试正常工作。
使用 expect(x).toHaveBeenCalledWith(someParams);
期望使用间谍,但我不知道如何监视同一文件中的函数...
Error: : Expected a spy, but got Function.
Usage: expect().toHaveBeenCalledWith(...arguments)
Example.ts
doSomething(word: string) {
if (word === 'hello') {
return this.somethingElse(1);
}
return;
}
somethingElse(num: number) {
var x = { "num": num };
return x;
}
Example.spec.ts
fake = {"num": "1"};
it('should call somethingElse', () => {
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
在您的 Example.spec.ts 中,只需添加 spyOn(component, 'somethingElse');
作为 it('should call somethingElse ...
测试的第一行:
fake = {"num": "1"};
it('should call somethingElse', () => {
// Add the line below.
spyOn(component, 'somethingElse');
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
expect 方法在 toHaveBeenCalledWith
之前使用时需要一个 Spy 作为参数(根据 Jasmine documentation)。