I need some help, I'm making some progress towards actually reading through a whole file, however, I've hit a decently sized roadblock (for me it's decent, I'm new to it). I'm trying to mock up most of a controller and I run into a problem when I get about half way through reading the file and I have no idea how to overcome it. I'll include as much code as I can below.
LocationController.js
angular
.module('TDE')
.controller('LocationController', ['$rootScope', '$scope', '$location', '$window', '$document', 'LocationService', 'HeaderFooterService', 'SearchService', 'TranslationService', 'MTDE_CONFIG', 'LocationPartnerAssignmentService', 'ExperimentService', function ($rootScope, $scope, $location, $window, $document, $LocationService, $HeaderFooterService, $SearchService, $TranslationService, $MTDE_CONFIG, $LocationPartnerAssignmentService, $ExperimentService) {
//passes over this code
$scope.init = function () {
// load dropdown for crop list
// load dropdown for business partner list
//load dropdown for experimenttype
//load dropdown for IrrigationType
//load dropdown for previousCrop
var locationId = $SearchService.GetLocationId();
$scope.LocationId=locationId;
var experimentId = $SearchService.GetExperimentId();
var experimentPartnerAssignmentId = $SearchService.GetExperimentPartnerAssignmentId();
//load nitrogen value
$ExperimentService.GetCrop(onSuccessCropCode,cropId);
// get location detail from database
if (locationId === "00000000-0000-0000-0000-000000000000") {
// Load the Unassigned Location here
$scope.IsAssinged = false;
}
else {
$LocationService.Detail(onSuccessDetail, locationId);
}
// get growing year list
$scope.GrowingYearList = $LocationService.GrowingYearList();
}
$scope.init(); //I NEED TO BE MOCKED SOMEHOW
Helper.js
ddescribe("Phases of Testing: The Journey", function () {
describe("Phase I: Test that Jasmine runs", function () {
it("should test Jasmine is up", function () {
expect(true).toBe(true);
});
});
describe("Phase II: Try something", function () {
var DBMock, SyncMock, baseMock, configMock, TransMock, loginMock, locMock, headerMock,
searchMock, LPAMock, expMock, mockScope, $location, $scope, ctrl, initMock;
beforeEach(function () {
angular.mock.module('TDE');
inject(function (_$location_, _$rootScope_, _$controller_) {
$location = _$location_;
$scope = _$rootScope_.$new();
ctrl = _$controller_('LocationController', {
'$scope': $scope
});
});
});
it("should be able to grab something", function () {
expect(true).toBe(true);
expect($scope).toBeDefined();
expect($scope.PlantingDateNull()).toBeTruthy();
});
});
});
EDIT: Now the problem comes down to mocking the $scope.init() function call after it is initially defined. I can't find a good source for help on this anywhere.
Application:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function ($scope, environment) {
$scope.testValue = 'test environment';
$scope.init = function() {
$scope.testValue = 'development environment';
};
environment.development && $scope.init();
});
myApp.factory('environment', function() {
return {
development: true
}
});
The most important line is environment.development && $scope.init();
environment.development
:
$scope.inint()
will be called$scope.inint()
To prove it I’m going to stub this servis with two values
Test:
beforeEach(function () {
module('myApp');
});
beforeEach(function () {
module(function ($provide) {
$provide.value('environment', mockEnvironment);
});
});
describe('MyCtrl', function () {
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controllerInstantiate = $controller;
}));
it('prevents call .init() on test environment', function () {
mockEnvironment.development = false;
createController();
expect(scope.testValue).toBe('test environment');
});
it('calls init on development environment', function () {
mockEnvironment.development = true;
createController();
expect(scope.testValue).toBe('development environment');
});
function createController() {
return controllerInstantiate('MyCtrl', {
'$scope': scope
});
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments