Mocking $cookies
Answer to SO question
HTML
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.js"></script>
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine-html.js"></script>
<link rel="stylesheet" href="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.css">
<script src="http://code.angularjs.org/1.2.0/angular.js"></script>
<script src="http://code.angularjs.org/1.2.0/angular-mocks.js"></script>
<script src="http://code.angularjs.org/1.2.0/angular-cookies.js"></script>
JavaScript
//--- CODE --------------------------
angular.module('myapp.test', [], function () {
}).controller('testcont', function($scope, $cookies) {
// Read a cookie and do something with it
$scope.favoriteCookie = $cookies.myFavorite;
});
// --- SPECS -------------------------
describe('controllers', function(){
var scope;
beforeEach(angular.mock.module('ngCookies', 'myapp.test'));
beforeEach(inject(function($controller, $rootScope, $cookies){
// Setting a cookie for testing
$cookies.myFavorite = 'oatmeal';
scope = $rootScope.$new();
$controller('testcont', {$scope:scope, $cookies: $cookies});
}));
it('sets the cookie on the scope', function(){
expect(scope.favoriteCookie).toBe('oatmeal');
});
});
// --- Runner -------------------------
(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();