我有像這樣一個構造函數的類: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
任何人都可以幫我解決嗎?
您可以提供堆棧跟蹤的完整異常信息sunch。另外CreateSsoLogin對'ILoginManager'做什麼? – aqwert
它沒有達到調用CreateSsoLogin時,SsoAuthenticationService實例化時代碼失敗 – jazza1000
您是否有任何靜態字段未在上面顯示? – aqwert