aftab
aftab

Reputation: 693

How to write test using Jasmine and AngularJS?

I am trying to write unit test for Post call, I have fake data in test suite that i am posting to endpoint but its giving me an error i attached to this question. I am new to Jasmine any idea to implement this test case better way will be appreciated.

So far tried code..

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

error

Now i am getting different error , please see updated error can't find variable : 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.

Upvotes: 2

Views: 105

Answers (1)

alaney
alaney

Reputation: 623

Looks like you're not passing prcsObj or an id to the saveProcess() method in your test and your not building your object in a way that the method expects. For instance you are missing the epcfUtilKeyList array in the prcsObj.

should look something like:

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

});

You could still be missing information, but based on the methods you posted this should work.

Upvotes: 1

Related Questions