【问题标题】:Make a module available in a controller that is tested by jasmine via Resharper在 jasmine 通过 Resharper 测试的控制器中使模块可用
【发布时间】: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


【解决方案1】:

我知道模拟服务的两种策略,具有多种语法变体,就像 Angular 中的所有内容一样...您可以简单地将对象文字添加到控制器声明中,或者您可以创建自己的服务并使用 $ 将它们添加到模块中提供者:

如果服务只是一些数据层或 API 的包装器,您可以使用文字对象模拟其功能,并在控制器构造函数中注入(如果正确),按照您的示例语法,可以像这样完成:

var currentAuth;

beforeEach(inject(function ($rootScope, $controller) {
    scope = $rootScope.$new();
    currentAuth = {uid: 1, name: juan, getFriends: function() { ... }};

    controller = $controller('TestableCtrl', {'$scope': $scope, 'currentAuth': currentAuth });
}));

在这种情况下,'currentAuth' 是一项服务,可在应用程序中提供当前登录的用户。

这仅在您不需要在对象中定义的函数中注入任何服务时才有效。这相当于创建一个价值服务并将其注入模块中。如果模拟服务中的方法本身需要任何服务,则必须创建工厂或服务,将此服务添加到模块中,然后像任何其他自定义服务一样注入它。请参阅我用来模拟 angularfire 身份验证服务的示例:

 var $controller, $rootScope, $scope, $location, Auth;

  beforeEach(function(){
    module('planner.login');

    module(function($provide){

      $provide.factory('Auth', function($q){
        return {
          $signInWithEmailAndPassword: function(email, pass) {
            return $q.reject({error: 'ERROR'});
          }
        };
      });

      return null;
    });

  });

  beforeEach(function(){

    inject(function($controller, $rootScope, _Auth_) {
      $scope = $rootScope.$new();
      Auth = _Auth_;

      $controller("TestableCtrl", {
        $scope: $scope,
        Auth: Auth,
        $stateParams: {}
      });
    });

  });

在这个示例中,我正在创建一个新工厂,它使用 $q 服务返回一个角度承诺(这不需要在 Chrome 上进行测试,但 PhantomJS 没有 Promise 规范)。请注意,为此您需要两个 beforeEach,一个用于将提供程序添加到模块中,另一个用于将提供程序注入到控制器中。

使用哪一个取决于您要测试什么以及模仿原始服务行为所需的深度。在您使用 uibmodal 的情况下,您可能需要在某个时候调用“.open”并监视已被调用的对象,但您只需要创建一个具有该属性的对象并为此监视对象属性。所以第一种方法应该足够了。

所以你的代码应该是这样的:

describe("Controllers", function() {

    beforeEach(module("moduleName"));

    describe("Jasmine  testableController", function () {

        var scope,
            controller,
            uibModal;

        beforeEach(inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            uibModal = { open: function() { return 'Whatever'; } }
            controller = $controller('testableController', { $scope: scope, $uibModal: uibModal });
        }));

        it('should set the page title as "Welcome"', function () {
            expect(scope.title).toBe('Welcome');
            // you will probably do something like this at some point to verify 
            // that the modal gets opened on click or following any other action:
            // var spy = spyOn(uibModal, 'open');
            // expect(spy).toHaveBeenCalled();
        });

    });
});

我希望它有所帮助,如果有不清楚的地方请告诉我,我也在学习测试 AngularJS 应用程序,很想知道这对 AngularUI 是否有帮助。

【讨论】:

    【解决方案2】:

    第一个问题是您没有将控制器注册到模块。所以它不知道模块的依赖关系。

    angular.module('modulename').controller('nameofthecontroller', controller);
    

    然后你可以模拟 $uibModal 但不是必需的;它应该可以在没有这个模拟的情况下工作;

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 2017-02-24
      • 2015-02-15
      • 1970-01-01
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      相关资源
      最近更新 更多