Mal*_*lik 5 javascript jasmine angularjs karma-runner karma-jasmine
我有一个UploadDocumentCtrl.js,在这里我正在访问另一个函数内部的函数的服务调用。此外部功能已绑定到范围。我的问题是我无法访问其中的代码语句(代码块C)。我想访问'flag'变量并检查是否为true。谁能指出我的正确方向或告诉我我在这里做错了什么?谢谢 ..
UploadDocumentsCtrl.js
(function () {
'use strict';
angular.module('myApp')
.controller('UploadDocumentsCtrl', UploadDocumentsCtrl);
UploadDocumentsCtrl.$inject = ['$rootScope', '$scope', '$modalInstance', '$window', 'companyService'];
function UploadDocumentsCtrl($rootScope, $scope, $modalInstance, $window, companyService) {
$scope.onFileSelect = onFileSelect;
$scope.buttonDisabled = false;
function onFileSelect(files) {
//Can access the code here
function upload(file) {
//Can access the code here as well
companyService.uploadDocuments(file)
.success(function (data, status, headers, config) {
// Code block C
// Cannot access any code here or the error code block below
$scope.flag = true;
})
.error(function (data, status, headers, config) {
});
}
files.forEach(function (file) {
file.progress = 0;
file.percent = 0;
$scope.filesToUpload.push(file);
upload(file);
});
}
}
})();
Run Code Online (Sandbox Code Playgroud)
茉莉花测试用例
(function () {
"use strict";
describe('UploadDocumentsCtrl', function () {
var scope, companyService,companyControllerFactory;
beforeEach(angular.mock.module('myApp'));
beforeEach(inject(function($rootScope, $controller , _companyService_) {
companyService = _companyService_;
scope = $rootScope.$new();
companyControllerFactory = function(){$controller('UploadDocumentsCtrl',
{
$scope: scope,
companyService: _companyService_
});
};
}));
describe("onFileSelect", function() {
it(" Should make the flag to true ", function() {
var files = [{}];
companyControllerFactory();
spyOn(companyService, 'uploadDocuments').and.returnValue({ success: function(){}});
scope.onFileSelect(files);
expect(scope.flag).toBe(true);
});
});
});
})();
Run Code Online (Sandbox Code Playgroud)
我在尝试执行上述操作时遇到的错误。
1)应该将标志设为true UploadDocumentsCtrl onFileSelect TypeError:在http:// localhost:9876 / absoluteC:/ Users / Documents / fle / Fle / WebApiRole中未定义companyService.uploadDocuments(...)。success(...)/app/company/UploadDocumentsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523(第31行)upload @ http:// localhost:9876 / absoluteC:/ Users / Documents / fle / Fle / WebApiRole / app / company / UploadDocumentsCtrl .js?f11d5dcacbf2ca1523523: onFileSelect / <@ http:// localhost:9876 / absoluteC:/ Users / Documents / fle / Fle / WebApiRole / app / company / UploadDocum entsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:51:17 onFileSelect @http:// localhost:9876 / absoluteC:/ Users / Documents / fle / Fle / WebApiRole / app / company / UploadDocumen tsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:46:13 @ http:// localhost:9876 / base / test / company / UploadDocumentsCtrlSpec.js?c5db561e203bdfae1a6f7509347d3f7032e8f785:35:17
小智 0
在我的项目中,我使用以下格式来监视服务。您可以尝试以下选项:
var deffered = q.defer();
spyOn(companyService, 'uploadDocuments').and.returnValue(deffered.promise);
deffered.resolve();
Run Code Online (Sandbox Code Playgroud)
要应用此功能,您必须在断言之前使用 $rootScope.$apply() (即在 Expect() 之前)