【问题标题】:Angular mocha test not firing success/errorAngular mocha 测试未触发成功/错误
【发布时间】:2016-04-01 12:30:43
【问题描述】:

我目前正在用 mocha 测试一个控制器。控制器有一个激活函数,它应该根据响应触发成功/失败。我无法在测试期间触发失败或成功函数。

viewController.js:

(function() {
'use strict';
angular
.module('app')
.controller('viewCtrl', viewCtrl);
function viewCtrl(Service) {
    vm.Service = Service;

    activate();
    function activate() {
        vm.Service.get().then(success, failure);
        function success(data) {
            if (!data || data == 401) {
                failure(data);
            }
        }
        function failure(error) {
            if (error) {
                console.error("Loading question failed:", error);
                vm.Service.set();
            }
        }
    }
}
})();

viewControllerTest.js:

describe('question_view_controller', function() {
    var httpBackend, controller;
    var expect = chai.expect;
    var assert = chai.assert;
    var Service = {};
    var createController;

    beforeEach(function(){
        angular.mock.module('ui.router');
        angular.mock.module('question');

        Service = {
            set : sinon.stub(),

            get : sinon.stub().returns(Promise.reject({error:{}}));
        }
    })

    beforeEach(inject(function($httpBackend,$controller,$q){
        httpBackend = $httpBackend;
        createController = function(){
            return $controller('ViewCtrl', {
                $scope: scope,
                Service: Service
            });;
        }
}));

afterEach(function(){
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

describe('activate', function () {
    describe('get.then() error', function(){
        beforeEach(function(){
            Service.get.returns(Promise.reject({error:{}}))
        })
        it('should do nothing and setFailedQuestion should be called once', function(){
            vm = createController();
            scope.$digest();
            expect(vm.Service.set.callCount).to.equal('1');
        })

    })

});

});

如果有人能指出我的错误或提供任何见解,那就太好了。有任何问题请追问。

更新: 编辑代码以反映 danday74 的答案。还是不行。

更新: 编辑代码以反映 danday74 的评论。还是不行。

【问题讨论】:

  • 在你的viewControllerTest.js中var QuestionService = {};应该是var Service = {};吗?否则,您在 beforeEach 中声明的“服务”可能与您在其他地方访问的“服务”不同......
  • 是的,应该。现在将修复。
  • 不抱歉仍然无法正常工作。
  • 我认为这不是问题的原因,但您在 viewCtrl 中也缺少 var vm = this;... viewCtrl 代码是否真的有效(在测试之外)?

标签: javascript angularjs unit-testing mocha.js sinon


【解决方案1】:

您需要调用范围摘要。您将需要注入 $rootScope 然后...

vm = createController();
$rootScope.$digest();
expect(vm.Service.set.callCount).to.equal('1');

$digest() 导致 THEN 块被执行。

类似于 $httpBackend.flush() 的方法,如果你曾经使用过的话。

【讨论】:

  • 感谢您的意见。更新了代码,但“expect(vm.service.get.callCount).to.equal('1')”仍然等于 false。
  • Service.get.returns(Promise.reject({error:{}})) - 在将 Service 传递到控制器之前,这应该在您的创建控制器函数中
  • 再次感谢您的意见。更新了我的问题以反映您的评论,但是“expect(vm.service.get.callCount).to.equal('1')”仍然等于 false。
  • 你的激活函数被调用了吗?大多数人也没有使用 Promise,而是使用 Angular 的 $q。 var def=$q.defer(); def.reject();返回def.promise; ...而且您在此处列出的代码仍然没有反映我建议的更改(这很好,但听起来您正在更新您的代码以显示您的最新版本)
  • 是的 active 被调用。如果我调用 vm.service.set() 处于活动状态,那么“expect(vm.service.get.callCount).to.equal('1')”等于 true。我完全错过了哪些变化。如果你告诉我,我会更新。再次感谢您的意见。
猜你喜欢
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多