{"id":1847,"date":"2015-01-02T13:15:20","date_gmt":"2015-01-02T11:15:20","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=1847"},"modified":"2014-12-29T15:03:35","modified_gmt":"2014-12-29T13:03:35","slug":"learning-angular-unit-testing-watch-expressions","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/","title":{"rendered":"Learning Angular: Unit Testing watch expressions"},"content":{"rendered":"<p><!-- blog posts ad --><\/p>\n<p>Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway, thx to a nice community member on the Angular IRC, I was able to quickly resolve the issue. So, here&#8217;s the story.<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>This article is part of my <strong>&#8220;Learning NG&#8221; series<\/strong>, presenting some of my adventures while learning Angular. Check out the <a href=\"http:\/\/www.webcodegeeks.com\/author\/juri-strumpflohner\/\">series intro and other articles<\/a>. <strong>Note,<\/strong> I&#8217;m an Angular newbie, so I&#8217;m more than happy for any kind of feedback and improvement suggestions from more experienced people than me.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>Background<\/h2>\n<p>The new Angular best practices suggest to use the &#8211; what they call &#8211; &#8220;controller as&#8221; syntax. So, instead of writing the controller like<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">module.controller('MainCtrl', function($scope){\r\n    $scope.someScopeVariable = 'Hello, world!';\r\n});<\/pre>\n<p>..you should instead write it like this.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">module.controller('MainCtrl', function(){\r\n    var vm = this; \/\/ this is a best practice approach\r\n    vm.someScopeVariable = 'Hello, world!';\r\n});<\/pre>\n<p>On the HTML side, you normally include the controller using a similar syntax.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;div ng-controller='MainCtrl as vm'&gt;\r\n    {{ vm.someScopeVariable }}\r\n&lt;\/div&gt;<\/pre>\n<h2>Problem<\/h2>\n<p>Now consider we have some watch expression defined in the controller, which we&#8217;d like to test.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">app.controller('MainCtrl', function($scope) {\r\n  var vm = this;\r\n  var previousSelection = null;\r\n\r\n  vm.currentSelection = null;\r\n\r\n  $scope.$watch('vm.currentSelection', function(newVal, oldVal){\r\n    \/\/ we'd like to test THIS LINE HERE\r\n    previousSelection = oldVal;\r\n  });\r\n\r\n  vm.changeSelection = function(shouldRevert){\r\n    if(shouldRevert){\r\n      vm.currentSelection = previousSelection;\r\n    }\r\n  };\r\n});<\/pre>\n<p>Note that I&#8217;m injecting <code>$scope<\/code> which might make it appear like I&#8217;m using the $scope controller syntax. In reality it&#8217;s for being able to register the <code>$watch<\/code>.<br \/>\nAlso, the above is a simple demo, which, slightly modified, can be useful for reverting a user selection on a dropdown for instance, using <code>ng-change<\/code>.<\/p>\n<p>Anyway, if we want to the the above, we could write the following test scenario.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">describe('Testing $watch expressions', function() {\r\n  var $scope = null;\r\n  var ctrl = null;\r\n\r\n  \/\/you need to indicate your module in a test\r\n  beforeEach(module('plunker'));\r\n\r\n  describe('using the controller as syntax', function() {\r\n\r\n    beforeEach(inject(function($rootScope, $controller) {\r\n      $scope = $rootScope.$new();\r\n\r\n      ctrl = $controller('MainCtrl', {\r\n        $scope: $scope\r\n      });\r\n\r\n    }));\r\n\r\n    it('test using $digest', function() {\r\n      \/\/ make an initial selection\r\n      ctrl.currentSelection = 'Hi';\r\n      $scope.$digest();\r\n\r\n      \/\/ make another one\r\n      ctrl.currentSelection = 'New';\r\n      $scope.$digest();\r\n\r\n      \/\/ simulate a ng-change which should revert to the previous value\r\n      ctrl.changeSelection(true);\r\n\r\n      expect(ctrl.currentSelection).toEqual('Hi');\r\n    });\r\n\r\n  });\r\n\r\n});<\/pre>\n<p>Note that I&#8217;m using <code>$scope.$digest()<\/code> after setting the <code>currentSelection<\/code> on the controller. This is needed to trigger a &#8220;digest cycle&#8221; which invokes the <code>$watch<\/code> expression I&#8217;ve defined. Unfortunately it <strong>doesn&#8217;t work!<\/strong>. The watch expression gets invoked, but <code>newVal<\/code> and <code>oldVal<\/code> are both undefined.<\/p>\n<p>Instead, if I reverted my controller to the &#8220;old&#8221; $scope syntax..<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">app.controller('MainCtrl', function($scope) {\r\n  var previousSelection = null;\r\n\r\n  $scope.currentSelection = null;\r\n\r\n  $scope.$watch('currentSelection', function(newVal, oldVal){\r\n    previousSelection = oldVal;\r\n  });\r\n\r\n  $scope.changeSelection = function(shouldRevert){\r\n    if(shouldRevert){\r\n      $scope.currentSelection = previousSelection;\r\n    }\r\n  };\r\n});<\/pre>\n<p>&#8230;and adjusted my tests accordingly:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">it('test using $digest', function() {\r\n    \/\/ make an initial selection\r\n    $scope.currentSelection = 'Hi';\r\n    $scope.$digest();\r\n\r\n    \/\/ make another one\r\n    $scope.currentSelection = 'New';\r\n    $scope.$digest();\r\n\r\n    \/\/ simulate a ng-change which should revert to the previous value\r\n    $scope.changeSelection(true);\r\n\r\n    expect($scope.currentSelection).toEqual('Hi');\r\n});<\/pre>\n<p>..then the $watch expression got called with the correct value and the tests passed as expected.<\/p>\n<p>Alternatively, I could leave the &#8220;controller As&#8221; syntax of before, and instead of calling <code>$scope.$digest()<\/code> in my tests, call <code>$scope.$apply('...')<\/code>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">it('test using $scope.$apply(...)', function() {\r\n    \/\/ make an initial selection\r\n    $scope.$apply('vm.currentSelection='Hi'');\r\n\r\n    \/\/ make another one\r\n    $scope.$apply('vm.currentSelection='New'');\r\n\r\n    \/\/ simulate a ng-change which should revert to the previous value\r\n    ctrl.changeSelection(true);\r\n\r\n    expect(ctrl.currentSelection).toEqual('Hi');\r\n});<\/pre>\n<p>That worked as well. <strong>What&#8217;s wrong here??<\/strong><\/p>\n<p>I posted on the <a href=\"http:\/\/echelog.com\/logs\/browse\/angularjs\/1416870000\">IRC channel<\/a>..<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&#x5B;14:32:52]  Interesting, when unit testing $watch expressions it makes a difference whether you used the 'controller as' syntax or not. http:\/\/plnkr.co\/edit\/MVOgfmXVG1MzUg6nfM6W?p=preview\r\n&#x5B;14:34:01]  of course - watch expressions watch on the scope.\r\n&#x5B;14:36:46]  sacho: yep, but by executing $scope.$digest() in the tests I'd expect that the $watch expression is executed...which, btw it is, but not with the correct values\r\n&#x5B;14:37:17]  sacho: Instead, it seems that in that case you have to do something like $scope.$apply('someScopeVar = 'some new value'');\r\n&#x5B;14:37:26]  then it fires as well, but with the passed new value\r\n&#x5B;14:37:35]  that's kinda odd..\r\n&#x5B;14:38:09]  while, when using the $scope syntax, I can simply call $scope.$digest() and everything works as expected...\r\n&#x5B;14:38:13]  huh?\r\n...\r\n&#x5B;14:46:08]  juristr, well, you're not placing the controller on the scope, anywhere.\r\n&#x5B;14:46:26]  so you're not using controllerAs.<\/pre>\n<p><strong>Oh..!<\/strong> The problem is in the <code>beforeEach<\/code>. While I was assuming the following lines attach the controller to the <code>$scope<\/code><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">beforeEach(inject(function($rootScope, $controller) {\r\n    $scope = $rootScope.$new();\r\n\r\n    ctrl = $controller('MainCtrl', {\r\n        $scope: $scope\r\n    });\r\n}));<\/pre>\n<p>..which they do&#8230;but the controller\/scope is not attached on the <code>vm<\/code> property, which the <code>$watch<\/code> expression expects&#8230;<\/p>\n<p>Thus, changing to&#8230;<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">beforeEach(inject(function($rootScope, $controller) {\r\n    $scope = $rootScope.$new();\r\n\r\n    ctrl = $controller('MainCtrl', {\r\n        $scope: $scope\r\n    });\r\n\r\n    \/\/ THIS was missing\r\n    $scope.vm = ctrl;\r\n}));<\/pre>\n<p>..makes everything work as expected, even when using <code>$scope.$digest()<\/code>.<\/p>\n<p>You can play around with it by yourself in this Plunkr.<\/p>\n<p><iframe src=\"http:\/\/embed.plnkr.co\/MVOgfmXVG1MzUg6nfM6W\/preview\" width=\"100%\" height=\"400px\"><\/iframe><\/p>\n<h2>Conclusion<\/h2>\n<p>This is actually quite tricky and easy to mistake, especially when you look at test examples which are running upon code that uses the somewhat older &#8220;scope syntax&#8221;. I&#8217;m not yet sure I wrapped my head around this issue yet&#8230;if I have a better explanation I&#8217;ll update the post&#8230;<\/p>\n<p>To summarize:<\/p>\n<ul>\n<li>use <code>$scope.$apply('theScopeVariable = \"new value\"')<\/code><\/li>\n<li>pay attention to the initialization of the controller in your unit test. If you&#8217;re using the controller as syntax, make sure you set it accordingly (see example before).<\/li>\n<\/ul>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/juristr.com\/blog\/2014\/11\/learning-ng-testing-watch-expressions\/\">Learning Angular: Unit Testing watch expressions<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/wcg\/\">WCG partner<\/a> Juri Strumpflohner at the <a href=\"http:\/\/juristr.com\/blog\/\">Juri Strumpflohner&#8217;s TechBlog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway, thx to a nice community member on the Angular IRC, I was able to quickly resolve the issue. So, here&#8217;s the story. &nbsp; This article is part of &hellip;<\/p>\n","protected":false},"author":5,"featured_media":909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-1847","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-02T11:15:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Juri Strumpflohner\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/juristr\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Juri Strumpflohner\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\"},\"author\":{\"name\":\"Juri Strumpflohner\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/33d3ee7edb105a80f1ff7199925b3060\"},\"headline\":\"Learning Angular: Unit Testing watch expressions\",\"datePublished\":\"2015-01-02T11:15:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\"},\"wordCount\":968,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"articleSection\":[\"Angular.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\",\"name\":\"Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"datePublished\":\"2015-01-02T11:15:20+00:00\",\"description\":\"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Angular.js\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Learning Angular: Unit Testing watch expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/33d3ee7edb105a80f1ff7199925b3060\",\"name\":\"Juri Strumpflohner\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/45338315375849845e4c4b30f52cb417221e2d9a7e688785e4dd2af0e624a260?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/45338315375849845e4c4b30f52cb417221e2d9a7e688785e4dd2af0e624a260?s=96&d=mm&r=g\",\"caption\":\"Juri Strumpflohner\"},\"description\":\"Juri Strumpflohner mainly operates in the web sector developing rich applications with HTML5 and JavaScript. Beside having a Java background and developing Android applications he currently works as a software architect in the e-government sector. When he\u2019s not coding or blogging about his newest discoveries, he is practicing Yoseikan Budo where he owns a 2nd DAN.\",\"sameAs\":[\"http:\/\/juristr.com\/blog\/\",\"http:\/\/linkedin.com\/in\/juristr\/\",\"https:\/\/x.com\/http:\/\/twitter.com\/juristr\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/juri-strumpflohner\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026","description":"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/","og_locale":"en_US","og_type":"article","og_title":"Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026","og_description":"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2015-01-02T11:15:20+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","type":"image\/jpeg"}],"author":"Juri Strumpflohner","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/juristr","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Juri Strumpflohner","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/"},"author":{"name":"Juri Strumpflohner","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/33d3ee7edb105a80f1ff7199925b3060"},"headline":"Learning Angular: Unit Testing watch expressions","datePublished":"2015-01-02T11:15:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/"},"wordCount":968,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","articleSection":["Angular.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/","name":"Learning Angular: Unit Testing watch expressions - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","datePublished":"2015-01-02T11:15:20+00:00","description":"Today I wanted to write a unit test for a watch expression on my controller. What seemed quite obvious initially, turned out to be quite nasty. Anyway,","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/learning-angular-unit-testing-watch-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Angular.js","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/"},{"@type":"ListItem","position":4,"name":"Learning Angular: Unit Testing watch expressions"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/33d3ee7edb105a80f1ff7199925b3060","name":"Juri Strumpflohner","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/45338315375849845e4c4b30f52cb417221e2d9a7e688785e4dd2af0e624a260?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/45338315375849845e4c4b30f52cb417221e2d9a7e688785e4dd2af0e624a260?s=96&d=mm&r=g","caption":"Juri Strumpflohner"},"description":"Juri Strumpflohner mainly operates in the web sector developing rich applications with HTML5 and JavaScript. Beside having a Java background and developing Android applications he currently works as a software architect in the e-government sector. When he\u2019s not coding or blogging about his newest discoveries, he is practicing Yoseikan Budo where he owns a 2nd DAN.","sameAs":["http:\/\/juristr.com\/blog\/","http:\/\/linkedin.com\/in\/juristr\/","https:\/\/x.com\/http:\/\/twitter.com\/juristr"],"url":"https:\/\/www.webcodegeeks.com\/author\/juri-strumpflohner\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1847"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1847\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/909"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}