我正在尝试为Post调用编写单元测试,我在测试套件中有假数据,我发布到端点但它给我一个错误我附加到这个问题。我是Jasmine的新手任何想法实现这个测试用例更好的方式将不胜感激。
到目前为止尝试过代码..
main.spec.js
describe('processFactory', function(){
'use strict';
var $httpBackend,Process;
beforeEach(module('riskAssessmentApp'));
beforeEach(inject(function(_$httpBackend_ , processFactory) {
$httpBackend = _$httpBackend_;
Process = processFactory;
}));
// make sure no expectations were missed in your tests.
afterEach(function() {
// $httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should be able to save process to the database',function(){
var epcfData = [{"type":"Epcf","id":2102}];
var erhData = [{"type":"Erh", "id":20001}];
var geoData =[{"type":"geoLocation", "id": 67000}];
var legalData = [{"type":"legal", "id":3358}];
var prcsObj = {
processLongName: "Process unit test 01",
processStatementText: "Process statement test 01",
epcfUtilKeyList: epcfData,
epcfDescription: "Epcf selected key description test 01",
businessSegmentOrControlFunction: "business segment test 01",
erhUtilKeyList: erhData,
geoLocationsKeyList: geoData,
legalEntitiesKeyList: legalData,
processOwnerWorkerKey: -1069,
prcsOwner: "xyz"
};
var promise = Process.saveProcess();
httpBackend = expectPOST('app/prcs/rest/process',prcsObj).respond(200,'success');
httpBackend.flush();
promise.then(function(res){
expect(res.data).toBe('success');
});
});
});
mainFactory.js
var serializeProcess = function (process) {
var objToReturn = {
processLongName: process.processLongName,
processStatementText: process.processStatementText,
epcfKey: process.epcfUtilKeyList[0].id,
epcfDescription: process.epcfDescription,
businessSegmentOrControlFunction: process.businessSegmentOrControlFunction,
erhKey: process.erhUtilKeyList[0].id,
geographicLocationKeyList: [],
legalEntityKeyList: [],
processOwnerWorkerKey: process.processOwnerWorkerKey,
prcsOwner: process.prcsOwner
};
saveProcess: function(process, id){
var request = serializeProcess(process);
console.log('request payload', JSON.stringify(request));
console.log('ID :: ', id);
// do this if you have differnet end point for save and update
//var endpoint = (id) ? 'app/prcs/rest/process/' + id : 'app/prcs/rest/process';
var endpoint = 'app/prcs/rest/process';
return $http.post(endpoint, request);
}
错误
现在我得到了不同的错误,请查看更新后的错误找不到变量:expectPost
LOG: 'request payload', '{"processLongName":"Process unit test 01","processStatementText":"Process statement test 01","epcfKey":2102,"epcfDescription":"Epcf selected key description test 01","busines
SegmentOrControlFunction":"business segment test 01","erhKey":20001,"geographicLocationKeyList":[67000],"legalEntityKeyList":[3358],"processOwnerWorkerKey":-1069,"prcsOwner":"xyz"}'
LOG: 'ID :: ', undefined
PhantomJS 1.9.8 (Windows 7) processFactory should be able to save process to the database FAILED
ReferenceError: Can't find variable: expectPOST
at C:/Users/spec/process/processFactory.spec.js:37
PhantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.015 secs / 0.023 secs)
Warning: Task "karma:coverage" failed. Use --force to continue.
答案 0 :(得分:1)
您似乎没有将prcsObj
或id
传递给测试中的saveProcess()
方法,而是以方法所需的方式构建对象。例如,您错过了epcfUtilKeyList
中的prcsObj
数组。
应该看起来像:
it('should be able to save process to the database',function(){
var epcfData = [{"type":"Epcf","id":2102}]
var prcsObj = {
processLongName: "Process unit test 01",
processStatementText: "Process statement test 01",
epcfUtilKeyList: epcfData,
epcfDescription: "Epcf selected key description test 01",
businessSegmentOrControlFunction: "business segment test 01",
erhUtilKeyList: [{ "id": 20001}],
processOwnerWorkerKey: 1254,
prcsOwner: "xyz"
};
var promise = Process.saveProcess(prcsObj);
$httpBackend.expectPOST('app/prcs/rest/process',prcsObj).respond(200,'success');
$httpBackend.flush();
promise.then(function(res){
expect(res.data).toBe('success');
});
});
您仍然可能缺少信息,但根据您发布的方法,这应该有效。