gpt4 book ai didi

unit-testing - 使用 $httpBackend 对 AngularJS Controller 进行单元测试

转载 作者:行者123 更新时间:2023-12-02 01:36:01 25 4
gpt4 key购买 nike

在我的一生中,我无法让 $httpBackend 在执行 $http get 请求的 Controller 上工作。我已经尝试了几个小时了 =)

我已将其简化为下面最简单的形式。如果我测试就通过

  • 注释掉 Controller 中的 $http.get() 请求
  • 注释掉测试中的“httpMock.flush()”
  • 并将“pig”和“dog”更改为匹配

也就是说,这是一个有效的、有效的测试和应用程序。

如果我把它放回去,我会收到底部显示的错误。

<小时/>

app/js/app.js

// Declare a module which depends on filters and services.
var myApp = angular
.module('myApp', ['ngRoute', 'myApp.filters', 'myApp.services',
'myApp.directives'])
.config(['$routeProvider' , function($routeProvider) {

$routeProvider
.when("/dashboard", {
templateUrl: "partials/dashboard.html",
controller: cDashboard
})

.otherwise({redirectTo: "/dashboard"});
}]);

// Pre-define our main namespace modules.
angular.module('myApp.directives' , []);
angular.module('myApp.filters' , []);
angular.module('myApp.services' , []);
angular.module('myApp.controllers', []);

app/js/controller.js

function cDashboard ($scope, $http) {
$scope.data = "dog";

// Fetch the actual data.
$http.get("/data")
.success(function (data) { $scope.data = data })
.error(function () {});
}

cDashboard.$inject = [ '$scope', '$http' ];

test/unit/controllerSpec.js

describe('cDashboard', function(){
var scope, ctrl, httpMock;

beforeEach(inject(function ($rootScope, $controller, $http, $httpBackend) {
scope = $rootScope.$new();
ctrl = $controller('cDashboard', {$scope: scope});

httpMock = $httpBackend;
httpMock.when("GET", "/data").respond("pig");
}));

it("should get 'pig' from '/data'", function () {
httpMock.expectGET("/data").respond("pig");
expect(scope.data).toBe("pig");
});

});

这是我在 shell 中遇到的错误:

INFO [watcher]: Changed file "/home/myApp/test/unit/controller/cDashboard.js".
Chrome 26.0 (Linux) cDashboard should get 'pig' from '/data' FAILED
Error: No pending request to flush !
at Error (<anonymous>)
at Function.$httpBackend.flush (/home/myApp/test/lib/angular/angular-mocks.js:1171:34)
at null.<anonymous> (/home/myApp/test/unit/controller/cDashboard.js:15:18)
Chrome 26.0 (Linux): Executed 1 of 1 (1 FAILED) (0.326 secs / 0.008 secs)

最佳答案

您的测试代码中有几个问题:

  1. Controller 是在 httpMock 配置为使用 pig 响应之前创建的。 expectGet 调用应该在实例化 Controller 之前发生。
  2. httpMock 需要刷新请求
  3. 只要您有 expectGet,就不需要 httMock.when

工作示例:http://plnkr.co/edit/lUkDMrsy1KJNai3ndtng?p=preview

describe('cDashboard', function(){
var scope, controllerService, httpMock;

beforeEach(inject(function ($rootScope, $controller, $httpBackend) {
scope = $rootScope.$new();
controllerService = $controller;
httpMock = $httpBackend;
}));

it("should get 'pig' from '/data'", function () {
httpMock.expectGET("/data").respond("pig");
ctrl = controllerService('cDashboard', {$scope: scope});
httpMock.flush();
expect(scope.data).toBe("pig");
});
});

关于unit-testing - 使用 $httpBackend 对 AngularJS Controller 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16427968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com