2

我正在尝试为使用 $http 服务获取文章详细信息的控制器编写单元测试。

控制器:

 .controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){

    $scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId;

    $http.get($scope.url).then(function(response) {
        //console.log(response.data);
        $scope.heading = response.data.Headline;
        $scope.rawBody = response.data.Body;
        $scope.body = $sce.trustAsHtml($scope.rawBody);
        $scope.image = response.data.Assets[0].URL;
    });   
    });

单元测试:

'use strict';

describe('Controller: Article', function () {

    var scope,
        $httpBackend,
        articleEndpoint;



    // load the controller's module
    beforeEach(module('myApp'));


    describe('ArticleDetailCtrl', function () {

        var ArticleDetailCtrl,
            jsonObject,
            ArticleId = '123';

        // Initialize the controller and a mock scope
        beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, Article, API_URL, ARTICLE_URL) {

            scope = $rootScope.$new();
            ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope });
            $httpBackend =  _$httpBackend_;
            articleEndpoint = API_URL + ARTICLE_URL + '/' + ArticleId;

            jsonObject = {
                'Headline': 'Headline',
                'Body': '<p>Body</p>',
                'Assets': [
                    {
                        'URL': 'path/to/image/article1.jpg'
                    }
                ]
            };

            $httpBackend.when('GET', articleEndpoint).respond(jsonObject);
        }));

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

        it('should fetch article details from the API', function () {
            //expect(scope.articles.length).toEqual(3);

            $httpBackend.expectGET(articleEndpoint);
            $httpBackend.flush();
        });

    });    

});

但我不断收到以下错误:

Error: Unexpected request: GET http://localhost:3000/api/articles/undefined
Expected GET http://localhost:3000/api/articles/123
    at $httpBackend (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1179)
    at sendReq (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:8181)
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:7921
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11319
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11405
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12412
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12224
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1438
    at /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:77
Error: Unsatisfied requests: GET http://localhost:3000/api/articles/123
    at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1472
    at /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:65

这是我第一次编写单元测试,随后我阅读了一些教程。我不知道我做错了什么?

4

1 回答 1

5

您的问题很简单,但很常见。

当您编写 Jasmine / Karma 单元测试时,使用$controller服务几乎可以自动创建控制器。这意味着,在大多数情况下,您可以从单元测试中说

$controller('ArticleDetailCtrl')

AngularJS 会找出它的依赖服务,注入它们并创建一个控制器实例供你进行单元测试。

有一个例外:

AngularJS 不知道如何注入特定于上下文的服务,例如 $scope 和 $routeParams,它们会根据控制器的每个实例的 HTML 结构和 URL 进行更改。

我注意到在您的单元测试中您将控制器创建为

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

所以此时,你告诉 AngularJS 当控制器请求 $scope 作为依赖服务时使用什么对象。但是在你的控制器中,你也注入了 $routeParams。并根据它的articleId创建请求的 URL。

因此,如果您将控制器实例化代码更改为:

ArticleDetailCtrl = $controller('ArticleDetailCtrl', { 
    $scope: scope, 
    $routeParams: articleId: ArticleId
});

现在应该创建正确的 URL(而不是使用未定义的 articleId,这是错误的)

让我知道这是否有帮助。

于 2014-08-21T08:03:07.993 回答