2017-06-01 149 views
2

運行單元測試項目時出現以下問題。無法獲得默認構造函數

無法獲取默認構造********類

[TestClass] 
public class PersonRegistration 
{ 
    private ILoggingService _loggingService; 
    private IUserManager _userManager; 
    public PersonRegistration(IUserManager userManager, ILoggingService loggingService) 
    { 
     this._userManager = userManager; 
     this._loggingService = loggingService; 
    } 
    [TestMethod] 
    public void TestMethod1() 
    { 
     RegisterBindingModel model = new RegisterBindingModel(); 
     AccountController ac = new AccountController(_userManager, _loggingService); 
     model.UserName = "[email protected]"; 
     var result = ac.Register(model); 
     Assert.AreEqual("User Registered Successfully", result); 
    } 

如何解決這個問題。一些答案說,使用參數少的構造函數。但在這裏我需要params。

RegisterBindingModel()

public class RegisterBindingModel 
{ 
    public RegisterBindingModel(); 
    [Display(Name = "User name")] 
    [Required] 
    public string UserName { get; set; } 
} 

問題Image

+1

Counstructor什麼類?你可以顯示'RegisterBindingModel'的構造函數嗎? –

+1

只需添加一個參數較少的構造函數。測試框架要求我相信。 – Wheels73

+0

您確定需要測試夾具參數的構造函數嗎?我相信你可以通過mock獲得所有的依賴關係,當然如果你正在進行單元測試 – hmnzr

回答

2

您應該使用像Moq嘲弄的框架。

例子:

[TestClass] 
public class PersonRegistration 
{ 

    [TestMethod] 
    public void TestMethod() 
    { 
     RegisterBindingModel model = new RegisterBindingModel(); 

     var mockService = new Mock<ILoggingService>();//Mock 
    //Do something as per your requirement 
    //var reg= new List<RegisterBindingModel >(); // provide some sample list 
    //mockService .Setup(r => r.GetAll=()).Return(reg); 

     var mockManager = new Mock<IUserManager>();//Mock 

    //Do something as per your requirement 
    //var user= new List<User>(); // provide some sample list 
    //mockManager .Setup(r => r.GetAll=()).Return(user); 

     AccountController ac = new AccountController(mockManager.Object, mockService.Object); 
     model.UserName = "[email protected]"; 
     var result = ac.Register(model); 
     Assert.AreEqual("User Registered Successfully", result); 
    } 
} 

你可以得到幫助的形式thisthis鏈接。

+0

您的第一個鏈接「Moq」轉到Socket.Available Property頁面。我認爲這是不正確的 – Dairo

2

我剛剛在我的單元測試中測試了這個。

添加

public PersonRegistration() 
{ 
} 

它應該運行正常。

您的單元測試類沒有必要使用構造函數。如果你正在使用像Moq這樣的模擬框架,那麼我使用工廠來爲我正在測試的類返回相關的moqs。

public ILoggingService ReturnMockLoggingService() 
{ 
     var mockService = new Mock<ILoggingService>(); 
     return mockService.Object; 
} 

然後在測試夾具中。

[TestMethod] 
public void TestMethod1() 
{ 
    RegisterBindingModel model = new RegisterBindingModel(); 

    var logService = MockFactory.ReturnMockLoggingService(); 
    var userService = MockFactory.ReturnMockUserService(); 
    AccountController ac = new AccountController(userService, logService); 

    model.UserName = "[email protected]"; 
    var result = ac.Register(model); 
    Assert.AreEqual("User Registered Successfully", result); 
} 

如果您不使用mock,那麼只需在測試中實例化用戶和日誌服務或創建SetUp。

[ClassInitialize] 
public void SetUp() 
{ 
    _loggingService = new LoggingService(); 
    _userManager = new UserManager(); 
} 

希望有所幫助。