编写 JavaScript 测试其他函数的测试被调用,而不是实际调用它们
Writing JavaScript tests that test other functions are called, without actually calling them
我的任务是为一些 AngularJS 由另一个团队编写的代码编写单元测试,他们没有编写任何测试
他们写了下面的函数,但我不知道如何测试它
function showCallAlerts(callRecord, isInEditMode, callBack) {
var callAlerts = populateCallAlertOnEditCall(callRecord.callAlert);
var callModalInstance = openAlertModalInstance('Call', callAlerts, callBack);
if (callModalInstance !== undefined && callModalInstance !== null) {
callModalInstance.result.then(function() {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
});
} else {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
}
}
我需要测试每个函数是否被调用,而不是担心它们的作用,因为我将单独测试它们,只是它们被调用了。
调用 populateCallAlertOnEditCall 时,它需要 return 空数组或其中包含一些项目的数组
调用 openAlertModalInstance 时,它需要 return undefined 或传递给 showEquipmentAlertsBasedOnCompanyDetails 的东西
showEquipmentAlertsBasedOnCompanyDetails 实际上应该被调用,我将单独测试该方法,只是它被调用了
我已经设法编写代码来测试简单的功能,但没有像这样的代码,所以任何帮助将不胜感激,我今天下午的大部分时间都在努力弄清楚
测试是否调用了什么,可以使用Spy
你的断言看起来像:
spyOn(obj, 'populateCallAlertOnEditCall')
expect(obj.method).toHaveBeenCalled()
更新:
populateCallAlertOnEditCall = {}
spyOn(obj, 'populateCallAlertOnEditCall.result')
expect(obj.method).toHaveBeenCalled()
你想要的那种行为叫做mocking
在 Jasmine 中,模拟是通过 Spy 对象完成的,您可以阅读更多关于这些的信息 here
基本上,您可以使用模拟来测试是否使用预期参数调用了函数。
var xhr = mock( XMLHttpRequest );
xhr.send();
expect( xhr.send ).toHaveBeenCalled();
您可以使用jasmine 来模拟您对测试不感兴趣的函数调用。例如,您可以在每次调用 'populateCallAlertOnEditCall' 时告诉 jasmine return 一个空数组。我将写一个示例,可能会让您有所了解:
describe('My Test Spec', function() {
var myController;
...
beforeEach( inject(($controller) => {
myController = $controller("myControllerName");
}));
it('Testing showCallAlerts when populateCallAlertOnEditCall returns an empty array', inject(function($controller) {
//setup
//this will replace every call to populateCallAlertOnEditCall with
//the function inside callFake
spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
return []; //returning an empty array.
});
//action
myController.showCallAlerts(...);
//assert
//Do your checking here.
}));
it('Testing showCallAlerts when populateCallAlertOnEditCall returns a non-empty array', inject(function($controller) {
//setup
//this will replace every call to populateCallAlertOnEditCall with
//the function inside callFake
spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
return [1,2,3,4]; //returning a non-empty array.
});
//action
myController.showCallAlerts(...);
//assert
//Do your checking here.
}));
});
我的任务是为一些 AngularJS 由另一个团队编写的代码编写单元测试,他们没有编写任何测试
他们写了下面的函数,但我不知道如何测试它
function showCallAlerts(callRecord, isInEditMode, callBack) {
var callAlerts = populateCallAlertOnEditCall(callRecord.callAlert);
var callModalInstance = openAlertModalInstance('Call', callAlerts, callBack);
if (callModalInstance !== undefined && callModalInstance !== null) {
callModalInstance.result.then(function() {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
});
} else {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
}
}
我需要测试每个函数是否被调用,而不是担心它们的作用,因为我将单独测试它们,只是它们被调用了。
调用 populateCallAlertOnEditCall 时,它需要 return 空数组或其中包含一些项目的数组
调用 openAlertModalInstance 时,它需要 return undefined 或传递给 showEquipmentAlertsBasedOnCompanyDetails 的东西
showEquipmentAlertsBasedOnCompanyDetails 实际上应该被调用,我将单独测试该方法,只是它被调用了
我已经设法编写代码来测试简单的功能,但没有像这样的代码,所以任何帮助将不胜感激,我今天下午的大部分时间都在努力弄清楚
测试是否调用了什么,可以使用Spy
你的断言看起来像:
spyOn(obj, 'populateCallAlertOnEditCall')
expect(obj.method).toHaveBeenCalled()
更新:
populateCallAlertOnEditCall = {}
spyOn(obj, 'populateCallAlertOnEditCall.result')
expect(obj.method).toHaveBeenCalled()
你想要的那种行为叫做mocking
在 Jasmine 中,模拟是通过 Spy 对象完成的,您可以阅读更多关于这些的信息 here
基本上,您可以使用模拟来测试是否使用预期参数调用了函数。
var xhr = mock( XMLHttpRequest );
xhr.send();
expect( xhr.send ).toHaveBeenCalled();
您可以使用jasmine 来模拟您对测试不感兴趣的函数调用。例如,您可以在每次调用 'populateCallAlertOnEditCall' 时告诉 jasmine return 一个空数组。我将写一个示例,可能会让您有所了解:
describe('My Test Spec', function() {
var myController;
...
beforeEach( inject(($controller) => {
myController = $controller("myControllerName");
}));
it('Testing showCallAlerts when populateCallAlertOnEditCall returns an empty array', inject(function($controller) {
//setup
//this will replace every call to populateCallAlertOnEditCall with
//the function inside callFake
spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
return []; //returning an empty array.
});
//action
myController.showCallAlerts(...);
//assert
//Do your checking here.
}));
it('Testing showCallAlerts when populateCallAlertOnEditCall returns a non-empty array', inject(function($controller) {
//setup
//this will replace every call to populateCallAlertOnEditCall with
//the function inside callFake
spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
return [1,2,3,4]; //returning a non-empty array.
});
//action
myController.showCallAlerts(...);
//assert
//Do your checking here.
}));
});