I an getting the below issue when running the Unit Test project.
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 = "[email protected]";
        var result = ac.Register(model);
        Assert.AreEqual("User Registered Successfully", result);
    }
How to fix that. Some answers says that to use a parameter less constructor. But here I need params.
RegisterBindingModel()
public class RegisterBindingModel
{
    public RegisterBindingModel();
    [Display(Name = "User name")]
    [Required]
    public string UserName { get; set; }
}
Issue 
I've just tested this in my unit tests.
Add
public PersonRegistration()
{
}
And it should run fine.
There is no need for constructors on your unit test classses. If you are using a mocking framework like Moq then I use a factory to return the dependent moqs for the classes I'm testing.
 public ILoggingService ReturnMockLoggingService()
 {
       var mockService = new Mock<ILoggingService>();
       return mockService.Object;
 }
Then in the test fixture.
[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);
}
if you're not using mocks then simply instance the user and log service in the test or create a SetUp.
[ClassInitialize]
public void SetUp()
{
    _loggingService = new LoggingService();
    _userManager = new UserManager();
}
Hope that helps.
You should use a mocking framework like Moq.
Example:
[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);
    }
}
You can get help form this and this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With