In Jasmine, how to test service response against static data

时间:2017-04-06 17:04:11

标签: angularjs jasmine

In angularJs app, I want to test the aboutService in jasmine. The About service returns data in the following format:

var companyObject = [{"CompanyName": "A", "Contact Number" : "123456789"}, {"CompanyName": "B", "Contact Number" : "1234567899"}]

angular
    .module('TestModule')
    .factory('aboutService', aboutService); 

    aboutService.$inject = ['$rootScope', '$http'];

    function homeService($rootScope, $http) {
        function getAboutData() {
            return $http({
                method: "GET",
                url: API URL,
            })
            .then(getAboutDataSuccess);

            function getAboutDataSuccess(results) {
                return results;//Data about company telephone contact number in array
            }
        }

        var aboutService = {
            getAboutData: getAboutData,
        };
    }

In aboutServiceSpec, I want to test if the data returned by API i.e an array length is greater than 0 and is equal to is equal to a variable companyObject

I have tried the following code with Jasmine, but it does not works.

    describe("Service: aboutService", function () {

        var aboutService, $httpBackend, $rootScope, response;
        var companyObject = [{ "CompanyName": "A", "Contact Number": "123456789" }, { "CompanyName": "B", "Contact Number": "1234567899" }]
        beforeEach(module('TestModule'));

        beforeEach(inject(function (_$httpBackend_, _aboutService_, _$rootScope_) {
            $httpBackend = _$httpBackend_;
            aboutService = _aboutService_;
            $rootScope = _$rootScope_;
        }));

        it('Should get about details of comapny', function () {
            $httpBackend.expectGET('URL');
            //Donot know how to go forward from here
        });
    });

1 个答案:

答案 0 :(得分:1)

$httpBackend.expectGET(...).respond(...)

编辑此代码的作用如下:

  1. .then定义期望 什么请求测试中的代码并嘲笑响应 请求
  2. 期望被$http块链接到。{ 承诺返回的服务(此时,承诺) $httpBackend.flush()请求尚未解决)
  3. .then结算 使用前面指定的模拟对象的promise(现在只是function calculateTotals(){ vm.sales.total_amount = {'_': 0, '_not': 0, '_part': 0, '_done': 0, 'n-p': 0}; vm.sales.total_price = {'_': 0, '_not': 0, '_part': 0, '_done': 0, 'n-p': 0}; vm.sales.order_total_price = {'_': 0, '_not': 0, '_part': 0, '_done': 0, 'n-p': 0}; vm.sales.order_total_amount = {'_': 0, '_not': 0, '_part': 0, '_done': 0, 'n-p': 0}; angular.forEach(vm.sales, function (sale) { angular.forEach(vm.sales.total_amount, function(value, key){ vm.sales.total_amount[key] += (sale.status == key) ? sale.total_amount * 1 : 0; vm.sales.total_price[key] += (sale.status == key) ? sale.total_price * 1 : 0; vm.sales.order_total_amount[key] += (sale.status == key) ? sale.order_total_amount * 1 : 0; vm.sales.order_total_price[key] += (sale.status == key) ? sale.order_total_price * 1 : 0; }); vm.sales.total_amount['_'] += sale.total_amount * 1; vm.sales.total_price['_'] += sale.total_price * 1; vm.sales.order_total_amount['_'] += sale.order_total_amount * 1; vm.sales.order_total_price['_'] += sale.order_total_price * 1; }); vm.sales.total_amount['_'] = vm.sales.total_amount['_'] - vm.sales.total_amount['n-p']; vm.sales.total_price['_'] = vm.sales.total_price['_'] - vm.sales.total_price['n-p']; vm.sales.order_total_amount['_'] = vm.sales.order_total_amount['_'] - vm.sales.order_total_amount['n-p']; vm.sales.order_total_price['_'] = vm.sales.order_total_price['_'] - vm.sales.order_total_price['n-p']; } 块中的期望被触发)。它也有 如果预期请求没有,则使测试无效的副作用 已经制作