2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

There should not be any id right because process is being saved first time.
Now i am getting different error i updated error in question its a dropdown that i think i have to create dummy data to pass to the test .. I am not sure how should pass dropdowns keys to the factory. Any idea
In your question I still don't see you passing your prcsObj but I assume you are. In that case the prcsObj has no property epcfUtilKeyLi. You didn't define it in your test. The error is telling you what's wrong. epcfUtilKeyLi is not defined.
epcfKey is the property that test is looking for and this is dropdown data in real scenario how can i create data for dropdown in test case and pass key from it
I am making progress i made change for all the dropdowns now you can see request payload but there is another error i attached to the question if you can help then my test will be executed hopefully. I updated my question with current implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.