I'm trying to test a function from a angularjs controller.
injection part:
var $scope, $controller;
beforeEach(module('sigfoxSDISApp'));
beforeEach(inject(function(_$controller_, $rootScope, _$log_, _$translate_, _DeviceService_, _$filter_, _translatableIndexFilter_){
$controller = _$controller_;
$scope = $rootScope.$new();
$controller = $controller('AcquitModalCtrl', {
$scope: $scope,
$log: _$log_,
$translate: _$translate_,
DeviceService: _DeviceService_,
$filter_: _$filter_,
translatableIndexFilter: _translatableIndexFilter_
});
}));
tests:
describe('$scope.getReport', function(){
this.success = 0;
this.errors = 0;
it('returns an object with a type, a title and a report', function(){
var result = $scope.getReport(this.success, this.errors);
expect(result.type).toBeDefined();
expect(result.title).toBeDefined();
expect(result.report).toBeDefined();
});
describe('getting type, report and title when success = 0 and errors = 1', function(){
this.errors = 1;
var result = $scope.getReport(this.success, this.errors);
it('returns a type error', function(){
expect(result.type).toEqual('error');
});
it('returns a report empty string', function(){
expect(result.report).toEqual('');
});
it('returns a title which is not empty string', function(){
expect(result.title).toMatch(/[a-z]/);
});
});
});
The first 'it' function works fine. In the 'getting type, report and title when success = 0 and errors = 1', i get the following error: "TypeError: Cannot read property 'getReport' of undefined"
So $scope is no more defined after the first 'it' function. I don't understand why.
From the Jasmine Doc
Nesting describe Blocks
Calls to describe can be nested, with specs defined at any level. This allows a suite to be composed as a tree of functions. Before a spec is executed, Jasmine walks down the tree executing each beforeEach function in order. After the spec is executed, Jasmine walks through the afterEach functions similarly.