gpt4 book ai didi

unit-testing - 测试异步功能给出了意外的请求

转载 作者:行者123 更新时间:2023-12-04 04:52:34 25 4
gpt4 key购买 nike

单元测试:

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

我正在测试的 Controller (工作):
init 中的异步函数给我带来了麻烦(使用 ../solr/users/select?indent=true&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. 创建一个 Controller (Search.get get 的执行)
4.通知后端接收所有请求并回答它们(刷新)

但是我总是收到以下错误:
Error: Unexpected request: GET : ../solr/users/select?indent=true&wt=json

我没有很好地处理异步搜索功能吗?这应该怎么做?

最佳答案

这不是真正的“单元”测试,它更像是一种行为测试。

这真的应该是一些测试:

  • 测试您的服务 Search.get 以确保它调用正确的 URL 并返回结果。
  • 测试您的 Controller 方法以确保它正在调用 Search.get
  • 测试您的 Controller 方法以确保它将结果放在正确的位置。

  • 您发布的代码有点不完整,但这里有两个单元测试应该涵盖您:

    这是我在博客上广泛讨论的内容,条目更详细:
  • Unit Testing Angular Controllers
  • Unit Testing Angular Services

  • 这是我正在谈论的一个例子:
    describe('Search', function () {
    var Search,
    $httpBackend;

    beforeEach(function () {
    module('myModule');

    inject(function (_Search_, _$httpBackend_) {
    Search = _Search_;
    $httpBackend = _$httpBackend_;
    });
    });

    describe('get()', function () {
    var mockResult;

    it('should call the proper url and return a promise with the data.', function () {
    mockResult = { foo: 'bar' };
    $httpBackend.expectGET('http://sample.com/url/here').respond(mockResult);

    var resultOut,
    handler = jasmine.createSpy('result handler');
    Search.get({ arg1: 'wee' }).then(handler);

    $httpBackend.flush();

    expect(handler).toHaveBeenCalledWith(mockResult);

    $httpBackend.verifyNoOutstandingRequest();
    $httpBackend.verifyNoOutstandingExpectation();


    });
    });

    });

    describe('myCtrl', function () {
    var myCtrl,
    $scope,
    Search;

    beforeEach(function () {
    module('myModule');

    inject(function ($rootScope, $controller, _Search_) {
    $scope = $rootScope.$new();
    Search = _Search;
    myCtrl = $controller('MyCtrl', {
    $scope: scope
    });
    });
    });

    describe('$scope.foo()', function () {
    var mockResult = { foo: 'bar' };

    beforeEach(function () {
    //set up a spy.
    spyOn(Search, 'get').andReturn({
    then: function (fn) {
    // this is going to execute your handler and do whatever
    // you've programmed it to do.. like $scope.results = data; or
    // something.
    fn(mockResult);
    }
    });

    $scope.foo();
    });

    it('should call Search.get().', function () {
    expect(Search.get).toHaveBeenCalled();
    });

    it('should set $scope.results with the results returned from Search.get', function () {
    expect(Search.results).toBe(mockResult);
    });
    });

    });

    关于unit-testing - 测试异步功能给出了意外的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18919409/

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