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

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


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();  });});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.css" rel="stylesheet" /><script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine-html.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/boot.js"></script><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.4/angular.min.js"></script><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.4/angular-mocks.js"></script>