如何使用 Jasmine 和 AngularJS 编写测试?

How to write test using Jasmine and AngularJS?

我正在尝试为 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.

看起来您没有将 prcsObjid 传递给测试中的 saveProcess() 方法,并且您没有以该方法期望的方式构建对象。例如,您在 prcsObj.

中缺少 epcfUtilKeyList 数组

应该看起来像:

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');
  });

});

您可能仍然缺少信息,但根据您发布的方法,这应该可行。