Jasmine + AngularJS单元测试休息呼叫

时间:2013-11-18 17:09:33

标签: unit-testing rest angularjs jasmine

我正在尝试为我的其余调用编写一些测试,并且我试图找出如何根据it声明进行唯一的休息调用。例如:

describe('My testing',function()
{  
    var $scope;
    var controller;
    var httpBackend;
    var http;  

    beforeEach(inject(function($rootScope,$controller,$http,$httpBackend)  
    {  
             $scope = $rootScope.new();  
             http = $http;  
             httpBackend = $httpBackend;
             httpBackend.when("GET","https://my/rest/call").respond([]);  
             controller = $controller('MyCtrl',{  
                 $scope:$scope,  
                 $http:$http  
             });  
    }));    

    it('my test',function()  
    {  
       expect($scope.results).toEquals([]);  
       httpBackend.flush();  
    });  
});

工作得很好。我怎样才能做到这一点,我可以在每个it中进行一次独特的通话?例如:

it('empty test',function()  
        {    
           httpBackend.when("GET","https://my/rest/call").respond([]);  
           expect($scope.results).toEquals([]);  
           httpBackend.flush();  
        });  


it('one test',function()  
        {    
           httpBackend.when("GET","https://my/rest/call").respond([{1:"one"}]);  
           expect($scope.results).toEquals([]);  
           httpBackend.flush();  
        });    

2 个答案:

答案 0 :(得分:2)

您可以使用expect代替when。与履行所有匹配请求的when不同,expect一次只能完成一个请求。因此,您可以告诉它您希望每次响应的内容。

请注意,如果请求的顺序与expect配置的顺序不同,则测试将失败。如果从未向expect中配置的请求发出请求,测试也将失败。

答案 1 :(得分:0)

首先删除。如果您希望在不同的响应中使用beforeEach,请在测试中定义它们。其次,实际上是对远程服务进行了调用。我只看到你正在设置当特定网址被击中时应返回的内容但是没有调用后端。这是两个测试样本

describe("inSearchDataSvc", function () {

    var inSearchDataSvc;

    beforeEach(module('app'));

    beforeEach(function () {

        //mockConfig = { dataServiceHostName: "http://myhost" };

        module(function ($provide) {
            //     $provide.value('config', mockConfig);
            //    $provide.value('$q', mockQ);
            //    $provide.value('config', mockConfig);
        });

        inject(function ($injector) {
            inSearchDataSvc = $injector.get('inSearchDataSvc');
        });

    });

      it("should return resolved promise", function () {

                $httpBackend.when('GET', /.*\/api\/insureds\/searchinsureds\?.*/).respond(200, { result: "result" });

                 var searchCriteria = new InsuredSearchCriteria();

                 $httpBackend.expectGET(searchUrl + "?clientId=&dateOfBirth=&emailAddress=&firstName=&includeExpiredInsureds=false&includeK12LineOfBusinessInsureds=false&lastName=&searchId=");

                var promiseResult;

                inSearchDataSvc.getInsureds(searchCriteria)
                    .then(function (result) {
                        promiseResult = result;
                    });

                $httpBackend.flush();

                expect(promiseResult.$resolved).toBe(true);
                expect(promiseResult.result).toBe("result");
            });

            it("it should reject promise if server throws and error", function() {

                $httpBackend.when('GET', /.*\/api\/insureds\/searchinsureds\?throwError=true/).respond(400, { error: "Bad Query" });

                var promiseResult;
                var returnedError;

                var searchCriteria = {throwError:true};

               $httpBackend.expectGET(searchUrl + "?throwError=true");

                inSearchDataSvc.getInsureds(searchCriteria)
                   .then(
                       function (result) { },
                       function (error) {
                       returnedError = error;

                   });

                $httpBackend.flush();

                expect(returnedError.data.error).toBe("Bad Query");
            });
});