function logOutUser() {
var url = "User/Logout";
httpWrapperService.post(url, {}, {}).then(function (success) {
},
function error(error) {
});
localStorageService.clearLocalStorage();
httpWrapperService.setAuthenticationToken(null);
}
以下是测试代码
'use strict';
describe('Testing registration-service\n\n', function () {
var httpBackend = null;
var baseURL = null;
var services = {
registration: null,
settings: null,
httpservice:null
};
// Initialization before tests
beforeEach(function () {
module("app");
// Inject needed services
inject(function ($httpBackend, _registrationService_, _settingsService_, _httpWrapperService_) {
services.registration = _registrationService_;
services.settings = _settingsService_;
httpBackend = $httpBackend;
httpservice=_httpWrapperService_;
//modal=$modal
// Get baseurl
baseURL = services.settings.getServicesURL();
// Default answer
httpBackend.whenGET("languages/en.json").respond("OK");
});
});
describe('Checking logOutUser() function for different test cases!!!\n', function () {
jasmine.createSpy('setAuthenticationToken').and.CallFake(function() {
//a fake implementation
});
console.log("setAuthenticationToken===", setAuthenticationToken);
it("1. When user is logged in \n", function (done) {
localStorage.SA_User='{"tokenId":"TICKET_1","rememberMe":true}';
var url = baseURL + "User/Logout";
httpBackend.whenPOST(url, {}).respond({});
expect(localStorage.SA_User).toEqual(undefined);
///expect(1).toEqual(1);
done();
});
});
});
我在注册服务(logoutuser)中定义了一个函数。我想对这个功能进行单元测试。我不想调用httpWrapperService.setAuthentication函数。 我怎么能这样做,我尝试使用callfake使用茉莉花和间谍,但却无法在业力中做到这一点。
任何帮助表示赞赏!!!! 感谢
答案 0 :(得分:1)
尝试:spyOn(services.httpservice, 'setAuthenticationToken').and.callFake(function(){})
而不是jasmine.createSpy
。这应该将间谍添加到服务中。