AngularJS,Jasmine和$ ressource =承诺未解决

时间:2015-11-20 15:55:46

标签: angularjs testing jasmine

我在模拟资源工厂之上构建茉莉花测试时遇到问题。

(工作)场景:

控制器:



.controller("remoteCtrl", function(resourceFactory) {
  var vm = this;

  vm.animals = resourceFactory.getAll();
});






        .factory('resourceFactory', function ($resource) {
            var demoResource = $resource('/demo-1.0/webresources/animal/:id', {id: '@id'}, {
                getAll: {method: 'GET', isArray: true},
                getFirst: {method: 'GET', params: {id: 0}, isArray: false},
                save: {method: 'POST', params: {id: null}}
            });

            return demoResource;
        });




用于Backendless开发的MockModule:



angular.module('playAngular.mock', ['ngMockE2E'])

        .run(function ($httpBackend, $log) {

            var animals = [
                {name: 'Animal1', age: '100', id: 0},
                {name: 'Animal2', age: '100', id: 1},
                {name: 'Animal3', age: '100', id: 2},
                {name: 'Animal4', age: '100', id: 3}
            ];

            $httpBackend.whenGET('/demo-1.0/webresources/animal').respond(function () {
                return [200, animals];
            });
  //....




这一切都很好。但现在问题点。我想用Jasmin测试我的控制器:



describe("Test Suite für RemoteController", function () {
    beforeEach(module('ngResource'));
    beforeEach(module('ngMockE2E'));
    beforeEach(module('playAngular.mock'));
    beforeEach(module('playAngular.remote'));
    
    var $controller;
    var $scope;
    var $rootScope;

    beforeEach(inject(function (_$controller_, _$rootScope_) {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
    }));

    var controller;
    beforeEach(function () {
        controller = $controller('remoteCtrl', {$scope: $scope});

    });

    describe("Basic Remote Test", function () {
        it('Test It:', function () {
            expect(controller).toBeDefined();
            expect(controller.animals).toBe(4);
        });
    });
});




测试将失败:

  

预期[$ promise:承诺({$$ state:Object({status:0})}),$ resolved:false]为4。

我理解错误,我们在这里有一个未解决的承诺,但我怎样才能让它等待并在茉莉花测试中测试repond? 这一个:

        controller.animals.$promise.then(function(result){
             expect(result.length).toBe(4);
        });

也不会起作用(茉莉花不会等待回复,只是回复"一切都很好"。

我尝试在web中找到的所有提示:触发摘要周期,刷新httpbackend,但没有任何效果。有人看到了这个问题吗?

0 个答案:

没有答案