【发布时间】:2016-12-22 17:42:19
【问题描述】:
我可以使用 Resharper 9.2 通过 PhantomJs 成功测试使用 jasmine 的控制器。作为一名测试人员。
我按照https://blogs.endjin.com/2014/09/unit-testing-angularjs-with-visual-studio-resharper-and-teamcity/ 上的说明设置了 Resharper。
这行得通:如果我不指定它所依赖的模块,我可以为控制器运行测试:
控制器:
var moduleName;
(function (moduleName) {
'use strict';
var testableController = (function () {
function testableController($scope) {
var _this = this;
this.$scope = $scope;
$scope.title = "Welcome";
}
testableController.className = 'testableController';
return testableController;
}());
moduleName.testableController = testableController;
})(moduleName || (moduleName = {}));
spec 文件如下所示
///<reference path="~/Scripts/jasmine/jasmine.js"/>
///<reference path="~/Scripts/jasmine/angular.js"/>
///<reference path="~/Scripts/jasmine/angular-mocks.js"/>
///<reference path="~/Scripts/angular-ui/ui-bootstrap.min.js" />
///<reference path="~/Scripts/jasmine/controllers.js"/>
///<reference path="~/Scripts/App/Controllers/testableController.js" />
///<reference path="~/Scripts/App/AppJasmine.js" />
describe("Controllers", function() {
beforeEach(module("moduleName"));
describe("Jasmine testableController", function () {
var scope,
controller;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('testableController', { $scope: scope });
}));
it('should set the page title as "Welcome"', function () {
expect(scope.title).toBe('Welcome');
});
});
});
真正的控制器使用 angular ui bootstrap "ui.bootstrap"。如果我将其更改为下一个示例,控制器将在页面上运行,但是当我尝试对其进行测试时出现错误
Error: [$injector:unpr] Unknown provider: $templateRequestProvider <- $templateRequest <- $uibModal
http://errors.angularjs.org/1.2.24/$injector/unpr?p0=%24templateRequestProvider%20%3C-%20%24templateRequest%20%3C-%20%24uibModal in http://localhost:61032/referenceFile?path=~/webui/trunk/Netvacation.Pegasus.WebUI/Scripts/jasmine/angular.js (line 3802)
依赖于 Bootstrap 的控制器
angular.module('moduleName', ['ui.bootstrap']);
var moduleName;
(function (moduleName) {
'use strict';
var testableController = (function () {
function testableController($scope, $uibModal) {
var _this = this;
this.$scope = $scope;
this.$uibModal = $uibModal;
$scope.title = "Welcome";
}
testableController.className = 'testableController';
return testableController;
}());
moduleName.testableController = testableController;
})(moduleName || (moduleName = {}));
**编辑 1 ** 我试过了
beforeEach(
function () {
module("ui.bootstrap");
module("moduleName");
}
);
但有同样的错误。
编辑 2 我用
http://angular-ui.github.io/bootstrap/ 版本:1.3.3 - 2016-05-22
AngularJS v1.2.24
编辑 3 我不想测试 $uibModal,但要模拟它
【问题讨论】:
-
你用的是什么版本的ui.bootstrap和什么版本的angular?
-
请查看更新后的问题 ui.bootstrap 1.3.3 , angular v1.2.24
标签: javascript angularjs jasmine resharper