我需要一些帮助,我在实际阅读整个文件方面取得了一些进展,然而,我已经遇到了一个规模不大的障碍(对我而言,它是体面的,我'我是新手)。我试图模拟大部分控制器,当我在阅读文件的大约一半时遇到问题,我不知道如何克服它。我将在下面提供尽可能多的代码。
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();
});
});
});
编辑: 现在问题归结为在最初定义之后模拟$ scope.init()函数调用。我无法在任何地方找到合适的帮助来源。
答案 0 :(得分:0)
您必须为 $ LocationService 而非 LocationService 提供模拟,并为Strips方法创建间谍,例如:
describe('myApp', function () {
var scope,
controller,
$mockLocationService = {};
beforeEach(function () {
module('myApp');
});
beforeEach(function () {
module(function ($provide) {
$provide.value('$LocationService', $mockLocationService);
});
});
describe('MyCtrl', function () {
beforeEach(function () {
$mockLocationService.Strips = jasmine.createSpy('$LocationService.Strips');
});
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MyCtrl', {
'$scope': scope
});
}));
it('calls Strips method', function () {
expect($mockLocationService.Strips).toHaveBeenCalled();
});
});
});
答案不再与编辑过的问题有关,抱歉。
答案 1 :(得分:0)
<强>应用强>
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
}
});
最重要的一行是environment.development && $scope.init();
environment.development
:
$scope.inint()
$scope.inint()
为了证明这一点,我将用两个值来存储这个服务
<强>测试强>
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
});
}
});