How to test John papa vm.model unit testing with jasmine?

后端 未结 3 1321
终归单人心
终归单人心 2021-01-01 14:12

I use John papa angular style guide my controller looks like:

following the style John papa style controller style guide:

function testController() {         


        
3条回答
  •  鱼传尺愫
    2021-01-01 14:21

    I would create a new module and inject it as a dependency into the app module to make it simple and testable. Testing the controller with John Papa's Style Guide:

    (function () {
      'use strict';
    
      angular
        .module('test')
        .controller('testController', testController);
    
      function testController() {
        var vm = this;
        vm.model = { name: "controllerAs vm test" };
      }
    })();
    

    The spec now would look like this:

    'use strict';
    
    describe('testController', function() {
      var testController;
      beforeEach(module('test'));
    
      beforeEach(function () {
        inject(function($injector) {
          testController = $injector.get('$controller')('testController', {});
        });
      });
    
      it('should have model defined and testController.name is equal to controllerAs vm test', function() {
        expect(testController).toBeDefined();
        expect(testController.name).toEqual("controllerAs vm test");
      });
    });
    

    Hope this helps anyone looking for similar solutions.

提交回复
热议问题