小编Yus*_*lii的帖子

在AngularJS应用程序中运行Karma单元测试时出现"意外请求"错误

我正在尝试为控制器编写单元测试,该控制器使用$ 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;
    });   
    });
Run Code Online (Sandbox Code Playgroud)

单元测试:

'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) { …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine angularjs karma-runner httpbackend

2
推荐指数
1
解决办法
3588
查看次数