Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了单元测试 – 测试asynchrone功能会产生意外请求大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
单位测试:

"use Strict";

var usersJSON = {};

describe("mainT",function () {


 var ctrl,scope,httpBACkend,LOCATIOnmock,beforeEach(module("testK"));
    beforeEach(inject(function ($controller,$rootScope,$httpBACkend,$LOCATIOn,$injector) {
        scope = $rootScope.$new();
        httpBACkend = $httpBACkend;
        LOCATIOnmock = $LOCATIOn;

        var lUrl = "../solr/users/SELEct?indent=true&wt=json",lrequestHandler = httpBACkend.expect("GET",lUrl);          
        lrequestHandler.respond(200,usersJSON);     

        ctrl = $controller("mainT.controller.users",{ $scope: scope,$LOCATIOn: LOCATIOnmock});
        httpBACkend.flush();
        expect(scope.users).toBeDefined();

    }));

    afterEach(function () {
        httpBACkend.verifyNoOutstandingrequest();
        httpBACkend.verifyNoOutstandingExpectation();
    });




        describe("method test",function () {
        it('should test',function () {
            expect(true).toBefalsy();
        });
    });
});

控制器我正在测试(工作):
init中的Asynchrone函数给我带来麻烦(使用../solr/users/SELEct?indent=true\u0026amp;wt=json):

$scope.search = function () {
                    var lStart = 0,lLimit = privates.page * privates.limit;


                    Search.get({
                        collection: "users",start: lStart,rows: lLimit)
                    },function(records){
                        $scope.users= records.response.docs;
                    });
                };

我认为发生了什么:

1.告知后端他将收到什么请求
2.通过空JSON通知后端对该请求的响应
3.创建一个控制器(Search.get get执行)
4.通知后端接收所有请求并回答(冲洗)

但我总是得到以下错误

Error: Unexpected request: GET : ../solr/users/SELEct?indent=true&wt=json

我不是很好地处理异步检索功能吗?该怎么做?

解决方法

在BeforeEach中,您应该使用httpBACkend.when而不是httpBACkend.expect.我不认为你的BeforeEach中应该有一个断言(expect),所以应该将它移到一个单独的it()块中.我也没有看到lrequestHandler的定义.认情况下会发送200状态,因此不需要.您的httpBACkend行应如下所示:

httpBACkend.when("GET","/solr/users/SELEct?indent=true&wt=json").respond({});

那么你的测试应该是:

describe("method test",function () {
        it('scope.user should be defined: ',function () {
            expect(scope.user).toEqual({});
        });
    });

大佬总结

以上是大佬教程为你收集整理的单元测试 – 测试asynchrone功能会产生意外请求全部内容,希望文章能够帮你解决单元测试 – 测试asynchrone功能会产生意外请求所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。