0

在测试中修复了很多错误之后,现在 Karma 输出是这样的:

PhantomJS 1.9.8 (Windows 8 0.0.0) 控制器:MainCtrl 应将事物列表附加到范围失败
错误:意外请求:GET /api/marcas 在 ....

api/marcas 是我创建的端点。MainCtrl 的代码:

'use strict';

angular.module('app')
.controller('MainCtrl', function ($scope, $http, $log, socket, $location, $rootScope) {
    window.scope = $scope;
    window.rootscope = $rootScope
    $scope.awesomeThings = [];
    $scope.things = ["1", "2", "3"];



    $http.get('/api/things').success(function(awesomeThings) {
        $scope.awesomeThings = awesomeThings;
        socket.syncUpdates('thing', $scope.awesomeThings);
    });


    $scope.addThing = function() {   
        if($scope.newThing === '') {
            return;
        }


        $http.post('/api/things', { name: $scope.newThing });
        $scope.newThing = '';
    };

    $scope.deleteThing = function(thing) {
        $http.delete('/api/things/' + thing._id);
    };

    $scope.$on('$destroy', function () {
        socket.unsyncUpdates('thing');


    });



    $http.get('/api/marcas').success(function(marcas) {
        $scope.marcas = marcas;
        socket.syncUpdates('marcas', $scope.response);

        $scope.marcasArr = [];

        $scope.response.forEach(function(value) {
            $scope.marcas.push(value.name);
        });

        $scope.marcaSel = function() {
            for (i = 0; i < $scope.response.length; i++) {
                if ($scope.selectedMarca == $scope.response[i].name) {
                    $scope.modelos = $scope.response[i].modelos;
                };
            };      
        };



    });
4

1 回答 1

0

直到你没有发布你的测试代码,我猜你的测试不包括以下代码:

beforeEach(inject(function () {
    httpBackend.expectGET('/api/marcas').respond(function(){
        return {/*some status code*/, {/*some data*/}, {/*any headers*/}};
    });
}));

如果 karma-runner 尝试执行您的测试代码,则需要向 $http-service、$http.get('/api/marcas') 和 $http.get('/api/things' )。如果不期望这些后端调用之一,则业力无法成功运行测试代码。

如果您不想为每个调用执行特殊操作,而只想为两个调用返回带有成功代码的默认值,则可以这样编写:

beforeEach(inject(function () {
    httpBackend.expectGET(/api/i).respond(function(){
        return {/*some status code*/, {/*some data*/}, {/*any headers*/}};
    });
}));
于 2015-11-23T15:50:55.193 回答