简体   繁体   English

AngularJs Jasmine单元测试中的$ httpBackend

[英]$httpBackend in AngularJs Jasmine unit test

I'm unable to get my unit test to work properly. 我无法让我的单元测试正常工作。 I have a $scope array that starts out empty, but should be filled with an $http.get(). 我有一个$ scope数组,它开始是空的,但应该用$ http.get()填充。 In the real environment, there'd be approx 15 or so objects in the array, but for my unit test I just grabbed 2. For the unit test, I have: 在真实的环境中,阵列中大约有15个左右的对象,但是对于我的单元测试,我只抓了2.对于单元测试,我有:

expect($scope.stuff.length).toBe(2);

But jasmine's error is: Expected 0 to be 2. 但茉莉的错误是:预期0为2。

here's my controller.js: 这是我的controller.js:

$scope.stuff = [];
$scope.getStuff = function () {
    var url = site.root + 'api/stuff';
    $http.get(url)
        .success(function (data) {
            $scope.stuff = data;
        })
        .error(function(error) {
            console.log(error);
        });
};

and my controller.spec.js is: 我的controller.spec.js是:

/// <reference path="../../../scripts/angular-loader.min.js" />
/// <reference path="../../../scripts/angular.min.js" />
/// <reference path="../../../scripts/angular-mocks.js" />
/// <reference path="../../../scripts/angular-resource.js" />
/// <reference path="../../../scripts/controller.js" />
beforeEach(module('ui.router'));
beforeEach(module('ngResource'));
beforeEach(module('ngMockE2E'));
beforeEach(module('myApplication'));
var $scope;
var controller;
var httpLocalBackend;

beforeEach(inject(function ($rootScope, $controller, $injector) {
    $scope = $rootScope.$new();
    controller = $controller("StuffController", {
        $scope: $scope
    });
}));

beforeEach(inject(function ($httpBackend) {
    httpLocalBackend = $httpBackend;
}));

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    expect($scope.stuff.length).toBe(2);
    //httpLocalBackend.flush();
} );

Now, obviously, I changed variable names and such since this is for work, but hopefully this is enough information for anybody to help me. 现在,显然,我更改了变量名称,因为这是为了工作,但希望这对任何人都有足够的信息来帮助我。 I can provide more if needed. 如果需要,我可以提供更多。 I also get a second error when uncommenting the .flush() line, but I'll get to that a bit later. 取消注释.flush()行时,我也会遇到第二个错误,但稍后我会谈到它。

Any help is greatly appreciated and thanks in advance! 非常感谢任何帮助,并提前感谢!

EDIT: 编辑:

Finally got it working! 终于搞定了! Here's my final code: 这是我的最终代码:

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

EDIT 2: I ran into another problem, which I think may have been the root cause of this. 编辑2:我遇到了另一个问题,我认为可能是这个问题的根本原因。 See Unit test failing when function called on startup 在启动时调用函数时,请参阅单元测试失败

You need to place httpLocalBackend.flush() statement before expect($scope.stuff.length).toBe(2). 您需要在expect($ scope.stuff.length).toBe(2)之前放置httpLocalBackend.flush()语句。 Once you make a request you need to flush it for the data to be available in your client code. 提出请求后,您需要刷新它以使客户端代码中的数据可用。

it('should get stuff', function () {
    var url = '/api/roles';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    scope.getStuff(); //Just moved this from after expectGET
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

Try this. 尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM