dimanche 1 février 2015

Unit testing angular service is not calling with expected param

I am testing a cancelWebSiteBuyRequest function that makes a call to a put request. I have done this many times but for some reason here I keep getting the error:



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' } } ].



I have done this the same way many times but I cannot find what I missed that causes the passed message to be called as the full object and not just the string I am pointing it too.


Function I am testing:



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


Test:



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

Aucun commentaire:

Enregistrer un commentaire