如何用jasmine测试自定义指令?

时间:2015-08-04 16:05:20

标签: angularjs unit-testing angularjs-directive jasmine

我在我的应用程序中制作了一个自定义指令,现在想用karma和jasmine进行单元测试,但我的测试失败了。 这是我的指示:

<div my-data remoteurl='url' filter='test' order-by='sortExpression' order='order'></div>

myappcontroller:

(function() {
    'use strict';

    var myApp = angular.module('myApp', [])
    .controller('myAppCtrl', ['$scope', '$http', function($scope, $http) {
        $scope.url = 'https://www.reddit.com/r/worldnews/new.json';
        $scope.filter= 'test';
        $scope.orderBy= 'sortExpression';
        $scope.order= 'orderBy';
    }])
    .directive('myData', ['$http', function($http) {
        return {
            restrict: 'A',
            scope: {
                remoteurl: '=',
                filter: '=',
                orderBy: '=',
                order: '='
                // orderBy:'sortExpression':'order' ;
            },
            templateUrl: 'DataTable.html',
            link: function(scope, element, attr) {

                $http.get(scope.remoteurl).success(function(response) {
                    scope.names = response.data.children;
                });
            }
        };
    }]);
})();

当我运行时,我会收到此错误:

       C:\wamp\www\First-angular-App>karma start karma.conf
        INFO [karma]: Karma v0.12.16 server started at        http://localhost:9876/
       INFO [launcher]: Starting browser Chrome
       INFO [Chrome 44.0.2403 (Windows 8)]: Connected on socket    TEd9Xo5aEr_WPAJCbfjc wi
      th id 79057650
      Chrome 44.0.2403 (Windows 8) unit testing directive order style should be false
     in the beginning FAILED
      Error: Unexpected request: GET DataTable.html
      No more request expected
         at $httpBackend (C:/wamp/www/First-angular-   App/bower_components/angu
      lar-mocks/angular-mocks.js:1178:9)

我该怎么做?任何建议将非常感谢,因为我已经被困在这里,所以指导我。我是棱角分明的新人。

1 个答案:

答案 0 :(得分:0)

你错过了

var views = [
    'DataTable.html'
];
views.forEach( function( view ) {
    beforeEach( module( view ) );
} );

httpBackend.expectGET( 'https://www.reddit.com/r/worldnews/new.json' ).respond( 200 );