{"id":15106965,"url":"https://github.com/normandy72/testing-controllers","last_synced_at":"2026-01-18T20:32:36.323Z","repository":{"id":153902835,"uuid":"593551561","full_name":"Normandy72/Testing-Controllers","owner":"Normandy72","description":"Testing AngularJS Controllers. Coursera course \"Single Page Web Applications with AngularJS\" by Yaakov Chaikin.","archived":false,"fork":false,"pushed_at":"2023-01-26T11:11:21.000Z","size":210,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-11T09:42:21.979Z","etag":null,"topics":["angular","angularjs","html","html5","jasmine","jasmine-tests","javascript","js","test","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Normandy72.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-26T09:40:29.000Z","updated_at":"2023-01-26T09:43:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"70c25073-7924-4da6-9c97-3d9b0f6fcdfa","html_url":"https://github.com/Normandy72/Testing-Controllers","commit_stats":{"total_commits":6,"total_committers":1,"mean_commits":6.0,"dds":0.0,"last_synced_commit":"215b66acbf66a01d47345093ffc7f58705817386"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Controllers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Controllers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Controllers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Controllers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Normandy72","download_url":"https://codeload.github.com/Normandy72/Testing-Controllers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345703,"owners_count":20924098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angularjs","html","html5","jasmine","jasmine-tests","javascript","js","test","testing"],"created_at":"2024-09-25T21:03:37.740Z","updated_at":"2026-01-18T20:32:36.316Z","avatar_url":"https://github.com/Normandy72.png","language":"JavaScript","readme":"# Testing AngularJS Controllers\n## - 1 -\n### beforeEach Setup\n```\ndescribe(\"tests\", function(){\n    \n    beforeEach(module('myApp'));\n});\n```\n`module('myApp')` - alias for angular.mock.module\n```\ndescribe(\"tests\", function(){\n\n    beforeEach(function(){\n        module('myApp');\n        setupMock();\n    });\n\n    // let's see what goes here in the next part of the code\n    ...\n});\n```\n```\n...\nvar $controller;\nvar mockService;\nvar myCtrl;\n\nbeforeEach(inject(function(_$controller_){\n    $controller = _$controller_;\n\n    // setting up a mock (fake) service\n    mockService = {};\n    mockService.aMethod = function(){\n        return 'fake-value';\n    };\n\n    myCtrl = $controller('MyCtrl', {MyService: mockService});\n}));\n...\n```\n`inject(...)` - alias for angular.mock.inject\n\n`_$controller_` - angular mock injector strips off the '_' \n\n### Test Method\n```\n    ...\n    it(\"should update init value on item add\", function(){\n        myCtrl.addItem();\n        experct(myCtrl.value).toBe(\"fake-value\");\n    });\n}); // end of describe\n```\n\n### Include JS Files in SpecRunner.html\n```\n\u003c!-- include source files here... --\u003e\n\u003cscript src=\"lib/angular.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"lib/angular-mocks.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"app.js\"\u003e\u003c/script\u003e\n\n\u003c!-- include spec files here... --\u003e\n\u003cscript src=\"spec/MyCtrl.spec.js\"\u003e\u003c/script\u003e\n```\n## - 2 -\n### beforeEach Setup\n```\nbeforeEach(function(){\n    module(function($provide){\n        $provide.service('MockService', function(){\n            var service = this;\n            service.aMethod = function(){\n                return 'fake-value';\n            };\n        });\n    });\n\n    module('MyApp');\n});\n...\n```\n```\n...\nvar $controller;\nvar myCtrl;\n\nbeforeEach(inject(function(_$controller_, MockService){\n    $controller = _$controller_;\n\n    myCtrl = $controller('MyCtrl', {MyService: MockService});\n}));\n```\n### Test Method\n```\n    ...\n    it(\"should update init value on item add\", function(){\n        myCtrl.addItem();\n        experct(myCtrl.value).toBe(\"fake-value\");\n    });\n}); // end of describe\n```\n***\n#### _Summary_\n* Angular provides ngMock module to help with unit testing.\n* To test a controller you need:\n    * Load module controller is in with `angular.mock.module('name');`.\n    * Use `$controller` to instantiate the controller you want to test.\n    * Use controller instance to invoke methods, access props, etc.\n* Do most of the setup in beforeEach. At least `module('name')` loading, but maybe more.\n* `$provide` service can be injected only into `module('name')`.\n* For other services, use angular.mock.inject method.\n***","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Ftesting-controllers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnormandy72%2Ftesting-controllers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Ftesting-controllers/lists"}