On Github Acrylico / Angular-Testing-Presentation
Created by Holden Profit ( https://github.com/Acrylico )
Completely independent, doesn't require a DOM, and has clean & clear syntax for testing all that JS code you've written. Documentation on Jasmine can be found at http://jasmine.github.io !
Everything in Jasmine starts with the SpecRunner.html file. It's within this file that you:
An example Angular controller, yet to be implemented:
myApp.controller('myController', function ($scope, $http) {
$scope.person = null;
$scope.initPerson = function (name, age, gender) { };
});
And here is our controller Spec file:
// Describe what this spec file is testing
describe("myController Tests", function () {
var scope, controller;
beforeEach(module("myApp")); // Creates the module "myApp"
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new(); // Creating a new scope for our controller
controller = $controller; // The service that instantiates controllers
controller("myController", {$scope: scope}); // Creates our ctrl object
}));
});
With TDD, you write your test FIRST then write your code to pass the test with minimal code and effort put forth to do so.
Given our controller's initPerson() function, lets write our test first, then complete the function to pass!
myApp.controller('myController', function ($scope) {
$scope.person = null;
$scope.initPerson = function (name, age, gender) { };
});
What should this function actually do?
describe("myController Tests", function () {
. . .
it("should fill the person field when initPerson() is called", function () {
// arrange
var name = "Guy Gardener", age = 30, gender = "male";
// act
scope.initPerson(name, age, gender);
// assert
expect(scope.person).toEqual({
name: name,
age: age,
gender: gender
});
});
});
Now that we know what the function needs to do, let's implement it!
myApp.controller('myController', function ($scope) {
$scope.person = null;
$scope.initPerson = function (name, age, gender) {
$scope.person = {
name: name,
age: age,
gender: gender
};
};
});
Feel free to contact me at: hprofit@icct.com Also look me up on github at https://github.com/Acrylico!