Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get Default Constructor

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 Image

like image 581
Harsha W Avatar asked Jun 01 '17 07:06

Harsha W


2 Answers

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.

like image 161
Wheels73 Avatar answered Nov 02 '22 23:11

Wheels73


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.

like image 39
Ashiquzzaman Avatar answered Nov 02 '22 23:11

Ashiquzzaman