单元测试 angular 服务未使用预期参数调用

Unit testing angular service is not calling with expected param

我正在测试一个调用放置请求的 cancelWebSiteBuyRequest 函数。我已经这样做了很多次,但出于某种原因,我一直收到错误消息:

Expected spy put to have been called with [ '/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel', { orgId : 19, webSiteId : 4, requestId : 5432 }, { message : 'this is my campaign post' } ] but actual calls were [ '/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel', { orgId : 19, webSiteId : 4, requestId : 5432 }, { message : { message : 'this is my campaign post' } } ].

我已经用同样的方法做了很多次,但我找不到我错过的东西,导致传递的消息被称为完整的对象,而不仅仅是我指向它的字符串。

我正在测试的功能:

this.cancelWebSiteBuyRequest = function (orgId, webSiteId, requestId, message) {
                var deferred = $q.defer();

            apiResource.put('/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel', { orgId: orgId, webSiteId: webSiteId, requestId: requestId }, { message: message })
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject("There was a problem canceling this buy request.");
            });

            return deferred.promise;
        };

测试:

describe('webSiteBuyRequestService', function () {

    beforeEach(module('pb.webSites.services'));

    var mockApiResourceProvider;

    var continuationToken = {
        nextPartitionKey: 4352632,
        nextRowKey: 613
    };

    var mockAccount = {
        //entityId: 'r43',
        //accountId: 436675,
        orgId: 19,
        page: 2,
        length: 12,
        requestId: 5432,
        campaignId: 3215,
        startDate: '05/02/25',
        endDate: '06/24/15',
        filterPath: '/:account/',
        filterRegion: 'North Amarica',
        webSiteId: 4,
        message: { message: "this is my campaign post" },
        buyRequestId: 5423
    };

    beforeEach(function () {
        mockApiResourceProvider = {
            hasError: false,
            get: function (url, params) {
                return {
                    then: function (callback, errorCallback) {
                        if (!mockApiResourceProvider.hasError) return callback(mockAccount);
                        return errorCallback();
                    }
                };
            },
            post: function (url, params, data) {
                return {
                    then: function (callback, errorCallback) {
                        if (!mockApiResourceProvider.hasError) return callback(data);
                        return errorCallback();
                    }
                };
            },
            put: function (url, params, data) {
                return {
                    then: function (callback, errorCallback) {
                        if (!mockApiResourceProvider.hasError) return callback(data);
                        return errorCallback();
                    }
                };
            },
            delete: function (url, params) {
                return {
                    then: function (callback, errorCallback) {
                        if (!mockApiResourceProvider.hasError) return callback();
                        return errorCallback();
                    }
                };
            }
        };

        module(function ($provide) {
            $provide.value('apiResource', mockApiResourceProvider);
        });

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

    });


describe('cancelWebSiteBuyRequest() function', function () {

        var baseURL = '/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel';

        var baseCall = { orgId: mockAccount.orgId, webSiteId: mockAccount.webSiteId, requestId: mockAccount.requestId };

        it("with no error", function () {
            spyOn(mockApiResourceProvider, "put").and.callThrough();
            var result = webSiteBuyRequestService.cancelWebSiteBuyRequest(mockAccount.orgId, mockAccount.webSiteId, mockAccount.requestId, mockAccount.message);

            expect(mockApiResourceProvider.put).toHaveBeenCalledWith(baseURL, baseCall, mockAccount.message);

            expect(result.$$state.status).toEqual(1);
            expect(result.$$state.value).toEqual(mockAccount.message);
        });
});

此外,如果我更改代码以使用 mockAccount.message.message,它会给我错误

Expected spy put to have been called with [ >'/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel', { orgId : 19, >webSiteId : 4, requestId : 5432 }, 'this is my campaign post' ] but actual >calls were [ '/:orgId/WebSites/:webSiteId/BuyRequests/:requestId/Cancel', { >orgId : 19, webSiteId : 4, requestId : 5432 }, { message : 'this is my campaign >post' } ].

您的产品代码正在用一个对象包裹您发送的消息:

, { message: message }

然后您将 mockAccount.message 作为参数发送。所以你最终得到:

{ message: { message: 'this is my campaign post' }};

我想你想发送 mockAccount.message.message(只是文本):

it("with no error", function () {
    spyOn(mockApiResourceProvider, "put").and.callThrough();
    var result = webSiteBuyRequestService.cancelWebSiteBuyRequest(mockAccount.orgId, mockAccount.webSiteId, mockAccount.requestId, mockAccount.message.message);

然后,在生产代码中,您将包装文本并产生:

{ message: 'this is my campaign post' }

然后匹配 mockAccount.message;