2013-10-23 69 views
0

我有像這樣一個構造函數的類:MOQ如何嘲笑依賴於其他類的類

public class SsoAuthenticationService : ISsoAuthenticationService 
{ 
    public SsoAuthenticationService(ILoginManager manager) 
    { 
     _manager = manager; 
    } 
} 

而且還管理器類有像這樣一個構造函數:

public class LoginManager:ILoginManager 
{ 
    private ILoginRepository login; 

    public LoginManager(ILoginRepository loginRepository) 
    { 
     login = loginRepository; 
    } 
    } 

我我試圖針對SSoAuthenticationService類編寫單元測試

public void CreateLogin_Returns_Login() 
    { 
     //arrange 
     var mocManager = new Mock<ILoginManager>(); 

     ILoginManager m = mocManager.Object; 

     //fails at the next line 
     var svc = new SsoAuthenticationService(mocManager.Object); 
     var request = new CreateSsoLoginRequest() 
          { 
           EmailAddress = "", 
           Password = "", 
           SecurityQuestionAnswer = "", 
           SecurityQuestionId = 0, 
           SiteIdentifier = "", 
           Username = "" 
          }; 

     //act 
     var response = svc.CreateSsoLogin(request); 

     //assert 
     response.Should().NotBeNull(); 
    } 

這會在嘗試立即嘗試時發出錯誤我認爲SsoAuthenticationService類("Value cannot be null.\r\nParameter name: value"),我相信問題是我還需要模擬IRepository類,但我不確定模擬代碼應該如何看待。

編輯:全異常信息

爲 'Progressive.Sso.WebServices.SsoAuthenticationService' 的類型初始值引發異常。
值不能爲空\ r \ n參數名:價值
在Progressive.Sso.WebServices.SsoAuthenticationService..ctor(ILoginManager經理)
在Progressive.Sso.Tests.Service_Methods.ServiceTest.CreateLogin_Returns_Login()在d: \ tfs \ sso \ Dev \ Dev \ Src \ SSO Web Services \ Progressive.Sso.Tests \ Service Methods \ ServiceTest.cs:line 29

任何人都可以幫我解決嗎?

+0

您可以提供堆棧跟蹤的完整異常信息sunch。另外CreateSsoLogin對'ILoginManager'做什麼? – aqwert

+0

它沒有達到調用CreateSsoLogin時,SsoAuthenticationService實例化時代碼失敗 – jazza1000

+1

您是否有任何靜態字段未在上面顯示? – aqwert

回答

0

我找到了一個解決方案,問題與Moq或構造函數依賴關係無關。

我有以下兩個成員變量聲明

private static readonly bool _useOperationTimer = SsoConfig.UseServiceOperationTimer; 
private static readonly bool _logRequestData = SsoConfig.LogRequestData; 

靜態方法調用了該問題的原因,如果他們被註釋掉一切正常。