{"id":1126,"date":"2014-10-29T14:15:56","date_gmt":"2014-10-29T12:15:56","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=1126"},"modified":"2014-10-29T14:05:40","modified_gmt":"2014-10-29T12:05:40","slug":"unit-test-angularjs-controller-with-jasmine","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/","title":{"rendered":"Unit Test AngularJS Controller With Jasmine"},"content":{"rendered":"<p><!-- [if lt IE 7]&gt;--><\/p>\n<p class=\"ng-scope\">Last time I demonstrated how to <a href=\"https:\/\/www.jiwhiz.com\/post\/2014\/4\/Consume_RESTful_API_With_Angular_HAL\" target=\"_blank\">Consume RESTful API With Angular-HAL<\/a>, and now I want to show you how to unit test my AngularJS controller code with<a href=\"http:\/\/jasmine.github.io\/\" target=\"_blank\">Jasmine<\/a>, a behavior-driven development framework for testing JavaScript code.<\/p>\n<h2 class=\"ng-scope\">BDD and Jasmine<\/h2>\n<p class=\"ng-scope\">I&#8217;m a big fan of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Test-driven_development\" target=\"_blank\">Test-Driven Development (TDD)<\/a>. And I always want to write unit test for my code. But not everyone agrees with me. There is a cost to write unit tests, and the benefit is in the long term, so it is not easy to convince others, especially managers or clients, that time spent on unit test will improve software quality and save money in the future. It is harder when developers write unit tests which don&#8217;t make sense. Sometime I even got frustrated when I tried to write unit test code for the sake of unit testing.<\/p>\n<p class=\"ng-scope\">So what exactly shall I test? How shall I write unit tests? <a href=\"http:\/\/dannorth.net\/introducing-bdd\/\" target=\"_blank\">Dan North introduced Behavior-driven development<\/a>, and he answered lots of my questions regarding the unit test. So what is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Behavior-driven_development\" target=\"_blank\">Behavior-Driven Development<\/a>?<\/p>\n<blockquote class=\"ng-scope\"><p>Behavior-driven development is about implementing an application by describing its behavior from the perspective of its stakeholders.<\/p><\/blockquote>\n<p class=\"ng-scope\">My understanding is, &#8220;BDD is TDD done right&#8221;. When we try to unit test a component, the first thing to think is how we want this component to behave, which is under different conditions, we expect it to do something or not to do something, to be in certain state or not to be in certain state.<\/p>\n<p class=\"ng-scope\">There are many JavaScript frameworks for unit test, like <a href=\"http:\/\/qunitjs.com\/\" target=\"_blank\">QUint<\/a>, <a href=\"http:\/\/visionmedia.github.io\/mocha\/\" target=\"_blank\">Mocha<\/a>, and <a href=\"http:\/\/jasmine.github.io\/\" target=\"_blank\">Jasmine<\/a>. The reason I choose Jasmine is the AngularJS example project <a href=\"https:\/\/github.com\/angular\/angular-seed\" target=\"_blank\">angular-seed<\/a> uses Jasmine for unit test. And I love it right after I figured out how to use it.<\/p>\n<p class=\"ng-scope\">With Jasmine, we first <code>describe<\/code> our test suite, and then use <code>it<\/code> function to give our test case specification. In the spec, we <code>expect<\/code> the behaviors of our testing target. We also use <code>beforeEach<\/code>and <code>afterEach<\/code> to setup or tear down test environment for our spec.<\/p>\n<h2 class=\"ng-scope\">Test Simple Controller<\/h2>\n<p class=\"ng-scope\">The best way to learn is to practice. Let&#8217;s start with a very simple AngularJS controller, for my <a href=\"https:\/\/www.jiwhiz.com\/#\/about\" target=\"_blank\">About<\/a>\u00a0page.<\/p>\n<p class=\"ng-scope\"><b><a href=\"https:\/\/github.com\/jiwhiz\/JiwhizBlogWeb\/blob\/master\/jiwhizblog-web\/app\/scripts\/public\/AboutController.js\" target=\"_blank\">AboutController.js<\/a><\/b><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n'use strict';\r\n\r\nangular.module('jiwhizWeb').controller('AboutController',\r\n&#x5B;'$rootScope', '$scope',\r\nfunction($rootScope, $scope) {\r\n    $rootScope.activeMenu = {\r\n        'home' : '',\r\n        'blog' : '',\r\n        'about' : 'active',\r\n        'contact' : '',\r\n        'admin' : ''\r\n    };\r\n    $rootScope.showTitle = true;\r\n\r\n    $rootScope.page_title = 'About Me';\r\n    $rootScope.page_description = 'Here is my story.';\r\n}\r\n]); <\/pre>\n<p>This is the simplest page in my blog application, and no complex logic in the controller code. It only sets the active menu item, shows the title and sets page title with description.<\/p>\n<p class=\"ng-scope\">The unit test for this controller will be also simple. We expect the page to do exactly what we designed, which are to set the &#8220;About&#8221; menu item as active, to show the title, and to have correct page title and description.<\/p>\n<p class=\"ng-scope\"><b><a href=\"https:\/\/github.com\/jiwhiz\/JiwhizBlogWeb\/blob\/master\/jiwhizblog-web\/test\/spec\/public\/AboutControllerSpec.js\" target=\"_blank\">AboutControllerSpec.js<\/a><\/b><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n'use strict';\r\n\r\ndescribe('Controller: public\/AboutController', function() {\r\n\r\n    var $rootScope, $scope, $controller;\r\n\r\n    beforeEach(module('jiwhizWeb'));\r\n\r\n    beforeEach(inject(function(_$rootScope_, _$controller_){\r\n        $rootScope = _$rootScope_;\r\n        $scope = $rootScope.$new();\r\n        $controller = _$controller_;\r\n\r\n        $controller('AboutController', {'$rootScope' : $rootScope, '$scope': $scope});\r\n    }));\r\n\r\n    it('should make about menu item active.', function() {\r\n        expect($rootScope.activeMenu.about == 'active');\r\n    });\r\n\r\n    it('should show title.', function() {\r\n        expect($rootScope.showTitle == true);\r\n    });\r\n\r\n    it('should have correct page title.', function() {\r\n        expect($rootScope.page_title).toEqual('About Me');\r\n    });\r\n\r\n    it('should have correct page description.', function() {\r\n        expect($rootScope.page_description).toEqual('Here is my story.');\r\n    });\r\n}); <\/pre>\n<p class=\"ng-scope\">In unit test code, we call Jasmine function <code>describe<\/code> for our test suite, and we pass the suite title string and function of our test suite. And inside this suite function, we call Jasmine function <code>it<\/code> for our test specs, and we also pass the spec title string and spec function. Inside our spec functions, we call<code>expect<\/code> function with actual value, which is chained with a Matcher function, which takes the expected value. Here I used <code>toEqual<\/code> matcher function. See <a href=\"http:\/\/jasmine.github.io\/2.0\/introduction.html\" target=\"_blank\">Jasmine Documentation<\/a> for more matcher functions and how to define your custom matcher functions.<\/p>\n<p class=\"ng-scope\">The first <code>beforeEach<\/code> is to load our application module &#8211; <code>jiwhizWeb<\/code>. And the second <code>beforeEach<\/code> is to inject dependencies from AngularJS, like <code>$rootScope<\/code>. Since we want to use same variable name<code>\"$rootScope\"<\/code> in our suite function, and let <code>$rootScope<\/code>be used by our spec functions, we have to apply a trick, that is to use underscore to wrap the in-coming parameter <code>_$rootScope_<\/code>, and AngularJS Mock will correctly resolve it to the reference to actual <code>$rootScope<\/code>. We can use it to create our suite variable <code>$scope<\/code> by <code>$scope = $rootScope.$new();<\/code>. Last, we use <code>$controller<\/code> to initialize our <code>AboutController<\/code> and inject all dependencies.<\/p>\n<h2 class=\"ng-scope\">Test Controller with Promise<\/h2>\n<p class=\"ng-scope\">OK, the <code>AboutController<\/code> is too simple. Let&#8217;s try another more complicated controller,<code>BlogListController<\/code>, which uses <a href=\"https:\/\/docs.angularjs.org\/api\/ng\/service\/$q\" target=\"_blank\">promise<\/a> to <a href=\"https:\/\/www.jiwhiz.com\/post\/2014\/4\/Consume_RESTful_API_With_Angular_HAL\" target=\"_blank\">Consume RESTful API With Angular-HAL<\/a>.<\/p>\n<p class=\"ng-scope\"><b><a href=\"https:\/\/github.com\/jiwhiz\/JiwhizBlogWeb\/blob\/master\/jiwhizblog-web\/app\/scripts\/public\/BlogListController.js\" target=\"_blank\">BlogListController.js<\/a><\/b><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n'use strict';\r\n\r\nangular.module('jiwhizWeb').controller('BlogListController',\r\n&#x5B;'$rootScope', '$scope', '$timeout', 'WebsiteService',\r\nfunction($rootScope, $scope, $timeout, WebsiteService) {\r\n    $rootScope.activeMenu = {\r\n        'home' : '',\r\n        'blog' : 'active',\r\n        'about' : '',\r\n        'contact' : '',\r\n        'admin' : ''\r\n    };\r\n    $rootScope.showTitle = true;\r\n\r\n    $rootScope.page_title = 'My Personal Blog';\r\n    $rootScope.page_description = 'Some of my thoughts and experiences.';\r\n\r\n    var setup = function( pageNumber ) {\r\n        WebsiteService.load()\r\n            .then( function( websiteResource ) {\r\n                return websiteResource.$get('blogs', {'page': pageNumber, 'size':10, 'sort':null});\r\n            })\r\n            .then( function( resource )\r\n            {\r\n                $scope.page = resource.page;\r\n                $scope.page.currentPage = $scope.page.number + 1;\r\n                return resource.$get('blogPostList');\r\n            })\r\n            .then( function( blogPostList )\r\n            {\r\n                $scope.blogs = blogPostList;\r\n                blogPostList.forEach( function( blog ) {\r\n                    blog.contentFirstParagraph = getFirstSection(blog.content);\r\n\r\n                    \/\/ load author profile\r\n                    blog.$get('author').then(function(author) {\r\n                        blog.author = author;\r\n                    });\r\n\r\n                });\r\n\r\n            })\r\n            ;\r\n\r\n    };\r\n\r\n    setup(0);\r\n\r\n    $scope.selectBlogPage = function(pageNumber) {\r\n        setup(pageNumber-1); \/\/Spring HATEOAS page starts with 0\r\n    };\r\n}\r\n]); <\/pre>\n<p class=\"ng-scope\">Here we have <code>setup<\/code> function to load list of blogs for specific page, and load each blog author. The challenge is how to test those chain of promises, which are asynchronous JavaScript operations. By searching <a href=\"http:\/\/stackoverflow.com\/questions\/16081586\/unit-test-promise-based-code-in-angular\" target=\"_blank\">Stack Overflow<\/a> and reading other developers&#8217; <a href=\"http:\/\/entwicklertagebuch.com\/blog\/2013\/10\/how-to-handle-angularjs-promises-in-jasmine-unit-tests\/\" target=\"_blank\">blog<\/a>, I found the way to use AngularJS<code>$q.defer()<\/code> to resolve the promise in unit test.<\/p>\n<p class=\"ng-scope\"><b><a href=\"https:\/\/github.com\/jiwhiz\/JiwhizBlogWeb\/blob\/master\/jiwhizblog-web\/test\/spec\/public\/BlogListControllerSpec.js\" target=\"_blank\">BlogListControllerSpec.js<\/a><\/b><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n'use strict';\r\n\r\ndescribe('Controller: public\/BlogListController', function() {\r\n    var $rootScope, $scope;\r\n    var $controller, service;\r\n\r\n    var mockWebsite = {\r\n        $get: function(rel) {}\r\n    };\r\n\r\n    var mockResource = {\r\n        page: {\r\n            size: 10,\r\n            totalElements: 100,\r\n            totalPages: 4,\r\n            number: 0\r\n        },\r\n        $get: function(rel) {}\r\n    };\r\n\r\n    var mockBlogPostList = &#x5B;\r\n        {\r\n            title: 'Test Blog',\r\n            content: '&lt;p&gt;This is first paragraph.&lt;\/p&gt; Other parts...',\r\n            $get: function(rel) {}\r\n        },\r\n        {\r\n            title: 'Another Blog',\r\n            content: '&lt;p&gt;I came second.&lt;\/p&gt;',\r\n            $get: function(rel) {}\r\n        }\r\n    ];\r\n\r\n    var mockAuthor = {\r\n        displayName: 'author'\r\n    };\r\n\r\n    beforeEach(module('jiwhizWeb'));\r\n\r\n    beforeEach(inject(function(_$rootScope_, _$controller_, _$q_, _WebsiteService_) {\r\n        $rootScope = _$rootScope_;\r\n        $scope = $rootScope.$new();\r\n        $controller = _$controller_;\r\n        service = _WebsiteService_;\r\n\r\n        var websiteDeferred = _$q_.defer();\r\n        websiteDeferred.resolve(mockWebsite);\r\n        spyOn(service, 'load').andReturn(websiteDeferred.promise);\r\n\r\n        var blogsDeferred = _$q_.defer();\r\n        blogsDeferred.resolve(mockResource);\r\n        spyOn(mockWebsite, '$get').andReturn(blogsDeferred.promise);\r\n\r\n        var blogListDeferred = _$q_.defer();\r\n        blogListDeferred.resolve(mockBlogPostList);\r\n        spyOn(mockResource, '$get').andReturn(blogListDeferred.promise);\r\n\r\n        var authorDeferred = _$q_.defer();\r\n        authorDeferred.resolve(mockAuthor);\r\n        spyOn(mockBlogPostList&#x5B;0], '$get').andReturn(authorDeferred.promise);\r\n        spyOn(mockBlogPostList&#x5B;1], '$get').andReturn(authorDeferred.promise);\r\n\r\n        $controller('BlogListController',\r\n            {'$rootScope' : $rootScope, '$scope': $scope, 'WebsiteService': service});\r\n        $rootScope.$apply(); \/\/ promises are resolved\/dispatched only on next $digest cycle\r\n    }));\r\n\r\n    it('should make Blog menu item active.', function() {\r\n        expect($rootScope.activeMenu.blog == 'active');\r\n    });\r\n\r\n    it('should have selectBlogPage() function.', function() {\r\n        expect($scope.selectBlogPage).toBeDefined();\r\n    });\r\n\r\n    describe('BlogListController setup(pageNumber) function', function() {\r\n\r\n        it('should have currentPage set to 1.', function() {\r\n            expect($scope.page.currentPage).toBe(1);\r\n        });\r\n\r\n        it('should have two blogs and first one is Test Blog.', function() {\r\n            expect($scope.blogs.length).toEqual(2);\r\n            expect($scope.blogs&#x5B;0].title).toEqual('Test Blog');\r\n        });\r\n\r\n        it('should have first blog with title &quot;Test Blog&quot; and author &quot;author&quot;', function() {\r\n            expect($scope.blogs&#x5B;0].title).toEqual('Test Blog');\r\n            expect($scope.blogs&#x5B;0].author.displayName).toEqual('author');\r\n        });\r\n\r\n        it('should have second blog with title &quot;Another Blog&quot; and author &quot;author&quot;', function() {\r\n            expect($scope.blogs&#x5B;1].title).toEqual('Another Blog');\r\n            expect($scope.blogs&#x5B;1].author.displayName).toEqual('author');\r\n        });\r\n\r\n    });\r\n}); <\/pre>\n<p class=\"ng-scope\">The trick is to use AngularJS deferred object by calling <code>$q.deferred()<\/code>, and use deferred API<code>resolve(value)<\/code> to return our mock resources. Then use Jasmine <code>spyOn()<\/code> function to stub our resource function and return promise object associated with the deferred object. After we setup all the mock up resources along the chain of promises, and initialize <code>BlogListController<\/code>, we have to do a final step <code>$rootScope.$apply();<\/code> to trigger AngularJS $digest cycle programmatically, so our promises can be resolved.<\/p>\n<h2 class=\"ng-scope\">Conclusion<\/h2>\n<p class=\"ng-scope\">Writing unit test is not easy. But it is so important, because writing unit test can actually make you think hard to write clean and elegant code.<\/p>\n<p class=\"ng-scope\">With <a href=\"http:\/\/en.wikipedia.org\/wiki\/Behavior-driven_development\" target=\"_blank\">behavior-driven development<\/a>, we can describe our test cases in a more understandable way, and it helps better communication between developers and business. Business can tell user stories, and developer can translate those stories into unit test, so when requirements change, we can easily find out what part of code we need to change as well.<\/p>\n<p class=\"ng-scope\">My experience with BDD and Jasmine is very pleasant, and later I will explore more about end to end test with <a href=\"https:\/\/github.com\/angular\/protractor\" target=\"_blank\">Protractor<\/a>.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"https:\/\/www.jiwhiz.com\/#\/blogs\/537399591d6ef7575fadf32a\">Unit Test AngularJS Controller With Jasmine<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/wcg\/\">WCG partner<\/a> Yuan Ji at the <a href=\"http:\/\/www.jiwhiz.com\/\">Jiwhiz<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code withJasmine, a behavior-driven development framework for testing JavaScript code. BDD and Jasmine I&#8217;m a big fan of Test-Driven Development (TDD). And I always want to write unit test for &hellip;<\/p>\n","protected":false},"author":17,"featured_media":918,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[44],"class_list":["post-1126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-js","tag-jasmine"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code\" \/>\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\/unit-test-angularjs-controller-with-jasmine\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\" \/>\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=\"2014-10-29T12:15:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-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=\"Yuan Ji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/jiwhiz\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yuan Ji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 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\/unit-test-angularjs-controller-with-jasmine\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\"},\"author\":{\"name\":\"Yuan Ji\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/d69825230c3e8af70cf459318487607e\"},\"headline\":\"Unit Test AngularJS Controller With Jasmine\",\"datePublished\":\"2014-10-29T12:15:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\"},\"wordCount\":1430,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg\",\"keywords\":[\"Jasmine\"],\"articleSection\":[\"Angular.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\",\"name\":\"Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg\",\"datePublished\":\"2014-10-29T12:15:56+00:00\",\"description\":\"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#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\":\"Unit Test AngularJS Controller With Jasmine\"}]},{\"@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\/d69825230c3e8af70cf459318487607e\",\"name\":\"Yuan Ji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/75e5ae238a8868d317d02621afc36017d01bfd867c5704dc7a64dcce40a240b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/75e5ae238a8868d317d02621afc36017d01bfd867c5704dc7a64dcce40a240b6?s=96&d=mm&r=g\",\"caption\":\"Yuan Ji\"},\"description\":\"Yuan is a passionate Java programmer and open source evangelist. He is eager to learn new technologies and loves clean and beautiful application design. He lives in Edmonton, Canada as an independent consultant and contractor.\",\"sameAs\":[\"http:\/\/www.jiwhiz.com\/\",\"http:\/\/www.linkedin.com\/in\/jiwhiz\",\"https:\/\/x.com\/https:\/\/twitter.com\/jiwhiz\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/yuan-ji\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026","description":"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code","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\/unit-test-angularjs-controller-with-jasmine\/","og_locale":"en_US","og_type":"article","og_title":"Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026","og_description":"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2014-10-29T12:15:56+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg","type":"image\/jpeg"}],"author":"Yuan Ji","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/jiwhiz","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Yuan Ji","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/"},"author":{"name":"Yuan Ji","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/d69825230c3e8af70cf459318487607e"},"headline":"Unit Test AngularJS Controller With Jasmine","datePublished":"2014-10-29T12:15:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/"},"wordCount":1430,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg","keywords":["Jasmine"],"articleSection":["Angular.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/","name":"Unit Test AngularJS Controller With Jasmine - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg","datePublished":"2014-10-29T12:15:56+00:00","description":"Last time I demonstrated how to Consume RESTful API With Angular-HAL, and now I want to show you how to unit test my AngularJS controller code","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/jasminejs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/unit-test-angularjs-controller-with-jasmine\/#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":"Unit Test AngularJS Controller With Jasmine"}]},{"@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\/d69825230c3e8af70cf459318487607e","name":"Yuan Ji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/75e5ae238a8868d317d02621afc36017d01bfd867c5704dc7a64dcce40a240b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/75e5ae238a8868d317d02621afc36017d01bfd867c5704dc7a64dcce40a240b6?s=96&d=mm&r=g","caption":"Yuan Ji"},"description":"Yuan is a passionate Java programmer and open source evangelist. He is eager to learn new technologies and loves clean and beautiful application design. He lives in Edmonton, Canada as an independent consultant and contractor.","sameAs":["http:\/\/www.jiwhiz.com\/","http:\/\/www.linkedin.com\/in\/jiwhiz","https:\/\/x.com\/https:\/\/twitter.com\/jiwhiz"],"url":"https:\/\/www.webcodegeeks.com\/author\/yuan-ji\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1126","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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1126"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/918"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}