Testing asynchrone function gives unexpected request

Unittest:

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

I test (works): Asynchrone function in init which gives me problems (uses ../ 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;
                    });
                };

What I think:
1. Tell the backend what request it will receive. 2. inform the backend about the answer to this request with an empty JSON
3. create a controller (Search.get get executed)
4. Inform the backend to receive all requests and respond to them (flash)

But I always get the following error:

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

Have I really missed the asynchronous image search function? how should this be done?

+4
source share
3

BeforeEach httpBackend.when httpBackend.expect. , () BeforeEach, 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({});
        });
    });
+2

"" , .

:

  • Search.get, , URL- .
  • , , Search.get
  • , , .

, , , , :

, , :

, :

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

});
+3

lUrl unit test , .. "../solr/users/select?indent=true&wt=json" "/Solr// = &? = JSON". , "http://localhost/a/b/index.html", lUrl "/a/solr/...".

, $httpBackend.expectGET(), , , .

+2
source

All Articles