Code coverage for answering a successful service call in karma

I have such a function

$scope.openMail=function(mail){ DocumentTypes.getDocument(function(response){ $scope.documentTypes=response.result; $log.log("documentTypes",$scope.documentTypes); }) } 

Range of fun above

 it("should test open mail", function(){ scope.documentTypes=[{"type":"pdf"},{"type":"xml"}]; spyOn(documentTypes,"getDocument").and.callFake(function(){ return scope.documentTypes; }); var mail='back'; scope.openMail(mail); expect(scope.documentTypes).toEqual({"type":"pdf"},{"type":"xml"}); }) 

therefore, the code does not apply to function(response){}

, so the code does not cover the code for <code> function (response) {} </ code> How can I cover this code in my code coverage? Thanks.

+5
source share
3 answers

You have several problems with your test:

  • You are doing spyOn(documentTypes,"getDocument") instead of spyOn(documentTypes,"getDocument")
  • Your fake function returns a value (synchronously) rather than calling the provided callback (asynchronous)
  • You start by initializing scope.documentTypes expected test result, i.e. the test passes no matter what the code does (unless you get an exception)
  • More problems with your code. The function you are testing does nothing with the mail input parameter

Here is how I would test it:

 describe('$scope.openMail', function() { beforeEach(function() { spyOn(DocumentTypes, 'getDocument'); }); it('uses DocumentTypes.getDocument service to get the document types', function() { $scope.openMail('test_mail'); expect(DocumentTypes.getDocument).toHaveBeenCalledWith(jasmine.any(Function)); }); describe('provides a callback function that', function() { beforeEach(function() { DocumentTypes.getDocument.and.callFake(function (callback) { callback('test_document_types'); }); }); it('stores the document types on the scope', function() { $scope.openMail('test_mail'); expect($scope.documentTypes).toEqual('test_document_types'); }); // Note: This is optional, depending on whether you test logging or not it('logs the document types', function() { spyOn($log, 'log'); $scope.openMail('test_mail'); expect($log.log).toHaveBeenCalledWith('documentTypes', 'test_document_types'); }); }); }); 
+1
source

I might be completely wrong here, but if this call to DocumentTypes.getDocument (...) is an asynchronous call, you might need to call the digest loop by calling scope.apply() right after calling scope.openMail(mail)

0
source

Assuming you enter DocumentTypes in your controller or whatever you open OpenMail in this function, you can make fun of it when you run tests. One way to do this might be to use the $ provision service.

This layout might look like this if you use karma-chai-spies:

 stubs = { DocumentTypes: { getDocument: chai.spy(function(callback) { callback({ result: [ "type" ] }); }) } }; 

Then provide it with $ security in your unit tests:

 beforeEach(function() { module(function($provide) { $provide.value("DocumentTypes", stubs.DocumentTypes); }); }); 

The test itself could look something like this using karma mocha and karma tea:

 it("should test open mail", function() { var controller = $controller('myController', { $scope: stubs.$scope }); stubs.$scope.openMail("mail"); expect(stubs.$scope.documentTypes).to.deep.equal([ "type" ]); expect(stubs.DocumentTypes.getDocument).to.have.been.called.once(); }); 
0
source

Source: https://habr.com/ru/post/1273385/


All Articles