$ httpBackend在AngularJs茉莉花单元测试 [英] $httpBackend in AngularJs Jasmine unit test

查看:527
本文介绍了$ httpBackend在AngularJs茉莉花单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的单元测试正常工作。我有开始是空的一个$范围数组,但应该充满了$ http.get()。在现实环境中,会是阵列中的约15个左右的物体,但对于我的单元测试我只是抓住2.对于单元测试,我有:

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(). 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:

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

但茉莉的错误是:预期0为2

But jasmine's error is: Expected 0 to be 2.

这是我的controller.js:

here's my 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);
        });
};

和我controller.spec.js是:

and my controller.spec.js is:

/// <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();
} );

现在,很明显,我改变变量名和这样的,因为这是工作,但希望这是足够的信息,任何人帮助我​​。如果需要,我可以提供更多。在取消对.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.

任何帮助是极大AP preciated并在此先感谢!

Any help is greatly appreciated and thanks in advance!

编辑:

终于得到了它的工作!这是我的最后code:

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);
} );

编辑2:
我遇到了另一个问题,我想可能是这样做的根本原因。见<一href=\"http://stackoverflow.com/questions/25794501/unit-test-failing-when-function-called-on-startup\">Unit试验失败时,呼吁启动功能

推荐答案

您需要将httpLocalBackend.flush()语句之前预期($ scope.stuff.length).toBe(2)。一旦你的要求,你需要刷新它的数据在客户端code可用。

You need to place httpLocalBackend.flush() statement before expect($scope.stuff.length).toBe(2). 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);
} );

试试这个。

这篇关于$ httpBackend在AngularJs茉莉花单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆