无法获取默认构造函数
Unable to get Default Constructor
我在 运行 单元测试项目时遇到以下问题。
Unable to get Default Constructor For class ********
[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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
如何解决这个问题。一些答案说要使用无参数构造函数。但是这里我需要参数。
RegisterBindingModel()
public class RegisterBindingModel
{
public RegisterBindingModel();
[Display(Name = "User name")]
[Required]
public string UserName { get; set; }
}
问题
你应该使用像 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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
您可以通过 this and this link.
获得帮助
我刚刚在单元测试中对此进行了测试。
添加
public PersonRegistration()
{
}
它应该 运行 没问题。
您的单元测试类不需要构造函数。如果您使用的是像 Moq 这样的模拟框架,那么我会使用工厂来 return 我正在测试的 类 的相关最小起订量。
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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
如果您不使用模拟,则只需在测试中实例化用户和日志服务或创建一个设置。
[ClassInitialize]
public void SetUp()
{
_loggingService = new LoggingService();
_userManager = new UserManager();
}
希望对您有所帮助。
我在 运行 单元测试项目时遇到以下问题。
Unable to get Default Constructor For class ********
[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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
如何解决这个问题。一些答案说要使用无参数构造函数。但是这里我需要参数。
RegisterBindingModel()
public class RegisterBindingModel
{
public RegisterBindingModel();
[Display(Name = "User name")]
[Required]
public string UserName { get; set; }
}
问题
你应该使用像 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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
您可以通过 this and this link.
获得帮助我刚刚在单元测试中对此进行了测试。
添加
public PersonRegistration()
{
}
它应该 运行 没问题。
您的单元测试类不需要构造函数。如果您使用的是像 Moq 这样的模拟框架,那么我会使用工厂来 return 我正在测试的 类 的相关最小起订量。
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 = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
如果您不使用模拟,则只需在测试中实例化用户和日志服务或创建一个设置。
[ClassInitialize]
public void SetUp()
{
_loggingService = new LoggingService();
_userManager = new UserManager();
}
希望对您有所帮助。