2016-02-09 142 views
0
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使用茉莉花和間諜,但不能在業力中做到這一點。

任何幫助表示讚賞! 謝謝

回答

1

嘗試:spyOn(services.httpservice, 'setAuthenticationToken').and.callFake(function(){})而不是jasmine.createSpy。這應該將間諜添加到服務中。

+0

我得到這個錯誤TypeError:注入服務時無法讀取屬性'setAuthenticationToken'null – shreyansh

+0

,您沒有將它設置爲'services.httpservice'。我認爲這是你想要做的。就像你做註冊和設置 – Gustav

+0

對不起,我沒有得到你,你能更清楚,我該怎麼辦? – shreyansh