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

后端 未结 3 1261
终归单人心
终归单人心 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

    The vm is equal to the instance itself via vm = this;

    Therefore, all the properties are hanging directly off of the object.

    function foo(){
      var vm = this;
    
      vm.name = 'Josh';
    }
    
    var myFoo = new foo();
    myFoo.name; // 'Josh';
    

    So all you need to do is change your expectations to remove the vm property.

    expect(testController).toBeDefined();  
    expect(testController.model).toBeDefined();     
    expect(testController.model.name).toEqual("controllerAs vm test");
    

    In order to prove this, here is your exact example, and the associated Jasmine tests.

    function testController() {
    
      var vm = this;
    
      vm.model = {
        name: "controllerAs vm test"
      };
    }
    
    
    angular.module('myApp', [])
      .controller('testController', testController);
    
    describe('Controller: testController', function() {
    
      beforeEach(module('myApp'));
    
      var testController;
    
      beforeEach(inject(function($controller) {
        scope = {};
    
        testController = $controller('testController', {});
    
      }));
    
      it('should have model defined and testController.model.name is equal to controllerAs vm test', function() {
        expect(testController).toBeDefined();
        expect(testController.model).toBeDefined();
        expect(testController.model.name).toEqual("controllerAs vm test");
      });
    
      it('should not have a property called vm', function() {
        expect(testController.vm).toBeUndefined();
      });
    });
    
    
    
    
    
    

提交回复
热议问题