Unable to get Default Constructor - c#

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 = "test123#gmail.com";
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 = "test123#gmail.com";
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 = "test123#gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
You can get help form this and this link.

Related

Mocked method do not pass correct value

I am trying to understand how mocking works in Xunit with AutoFixture. I have created Service and Repository classes and their interfaces. Mocked method should pass value which is different from default value.
Mocked method always pass default values instead of values which i am writing in ".Returns()". I have tried AutoConfiguredMoqCustomization but it provides completely random values which i can't get back.
Repository.cs
public class Repository : IRepository
{
public int GetInt()
{
return 999;
}
}
Service.cs
public class Service : IService
{
private readonly Repository _repository;
public Service()
{
_repository = new Repository();
}
public string GetStringFromInt()
{
return _repository.GetInt().ToString();
}
}
Test
[Fact]
public void Test()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var repositoryMock = fixture.Create<Mock<IRepository>>();
var service = fixture.Create<Service>();
repositoryMock.Setup(x => x.GetInt()).Returns(1);
var act = service.GetStringFromInt();
Assert.Equal("1", act);
}
As you see value by default in Repository is 999 and I am expecting 1 from repositoryMock but result is "999" instead of "1".
Ow I have understood my problem. When I declare parameters with auto moq testing service must be AFTER all mocked repositories
Test
[Theory, AutoMoqData]
public void Test([Frozen] Mock<IRepository> repositoryMock, Service service)
{
...
}
Attribute
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(GetDefaultFixture)
{
}
private static IFixture GetDefaultFixture()
{
return new Fixture().Customize(new AutoMoqCustomization());
}
}
You should freeze your mock first. When you call Create on AutoFixture, it will create you a new instance every time. Try the following in the modified code (where you are using an interface of the type in your constructor).
public class ServiceTests
{
private readonly IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
public ServiceTests()
{
fixture.Register<IService>(() => fixture.Create<Service>());
}
[Fact]
public void Test()
{
// Arrange
var repositoryMock = fixture.Freeze<Mock<IRepository>>();
repositoryMock.Setup(x => x.GetInt()).Returns(1);
var service = fixture.Create<IService>();
// Act
var act = service.GetStringFromInt();
// Verify
Assert.Equal("1", act);
}
}
To check that you have set up autofixture correctly, you can try the following in the unit test in future.
var repo1 = fixture.Create<IRepository>();
var repo2 = fixture.Create<IRepository>();
Assert.Equal(repo1.GetHashCode(), repo2.GetHashCode());
If the above fails, that indicates that you have not frozen a type. These lines of code have saved me so much head scratching in the past...
You are doing DI wrong, you are not injecting Repository into Your serice.
Try like this.
public class Repository : IRepository
{
public int GetInt()
{
return 999;
}
}
public class Service : IService
{
IRepository Repository;
public Service(IRepository repository)
{
this.Repository = repository;
}
public string GetStringFromInt()
{
return Repository.GetInt().ToString();
}
}
Now when you mock IRepository, you can add it to Service.
You are using a new Repository() in constructor, so you are using that implementation

How to Unit Test a GlassController Action without SitecoreContext Dependency Injection

I'm a sitecore developer, and I want to create a sample sitecore helix unit testing project for testing out the exact below Index() action method of our "HomeBottomContentController" controller, without any dependency injection into a constructor. Note that the commented-out code is exactly what I do NOT want to do.
public class HomeBottomContentController : GlassController
{
// I want to test the EXACT method below
public override ActionResult Index()
{
var context = new SitecoreContext();
var model = context.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
/*
// I do NOT want to have any of the below code for injecting ISitecoreContext into a constructor and testing the IndexTest() below it.
private readonly ISitecoreContext _iSitecoreContext;
public HomeBottomContentController(ISitecoreContext iSitecoreContext)
{
_iSitecoreContext = iSitecoreContext;
}
public HomeBottomContentController()
{ }
public ActionResult IndexTest()
{
var model = _iSitecoreContext.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
*/
}
Here's what I have in my unit testing class. Again, what I have commented out exactly what I don't want to do:
[TestClass]
public class MvcUnitTests
{
[TestMethod]
public void Test_HomeBottomContentController_With_ISitecoreContext()
{
/*
// I don't want to do below...
var model = new Home_Control()
{ Bottom_Content = "XYZ" };
var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>();
iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model);
HomeBottomContentController controllerUnderTest = new HomeBottomContentController(iSitecoreContext.Object);
var result = controllerUnderTest.IndexTest() as ViewResult;
*/
//I want to test using the exact constructor below and calling that exact Index() method.
HomeBottomContentController controllerUnderTest = new HomeBottomContentController();
var result = controllerUnderTest.Index() as ViewResult;
Assert.IsNotNull(result);
Assert.IsNotNull(result.Model);
//Assert.AreEqual(((Home_Control)result.Model).Bottom_Content, "XYZ");
}
}
How can I test my controller's exact Index() method without having to add code to the HomeBottomContentController class that enables dependency injection into a constructor (like the commented-out code above)? I do not want to have to add code to HomeBottomContentController().
#Aleksey Shevchenko If I try your solution, how do I exactly hook up the model to the iSitecoreContext and then assign that to the controllerUnderTest.FakeContext? My code below throws compilation error (You cannot convert from Mock of Glass.Mapper.Sc.ISitecoreContext to Glass.Mapper.Sc.ISitecoreContext, how do we accomplish that):
var model = new Home_Control()
{ Top_Content = "Some Dummy Test Home Top Content" };
var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>();
//var iSitecoreContext = new Glass.Mapper.Sc.SitecoreContext();
iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model);
FakeHomeTopContentController controllerUnderTest = new FakeHomeTopContentController();
controllerUnderTest.FakeContext = (Glass.Mapper.Sc.SitecoreContext)iSitecoreContext;
If you don't want to create dependency injection through constructor you can do that through protected virtual method. Something like that:
public class HomeBottomContentController : GlassController
{
public override ActionResult Index()
{
var context = this.GetContext();
var model = context.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
protected virtual SitecoreContext GetContext()
{
return new SitecoreContext();
}
}
[TestClass]
public class MvcUnitTests
{
[TestMethod]
public void Test_HomeBottomContentController_With_ISitecoreContext()
{
var controllerUnderTest = new FakeHomeBottomContentController();
controllerUnderTest.FakeContext = /* set what you want */;
var result = controllerUnderTest.Index() as ViewResult;
Assert.IsNotNull(result);
Assert.IsNotNull(result.Model);
}
public class FakeHomeBottomContentController : HomeBottomContentController
{
public SitecoreContext FakeContext { get; set; }
protected override SitecoreContext GetContext()
{
return this.FakeContext;
}
}
}
I don't know the limitations of your ability to end the source code but based on your comment to the other answer you have access to it. I would opt for Poor Man's DI, have two constructors on the controller. The unit test uses the second constructor and MVC will use the parameterless constructor.
public class HomeBottomContentController : GlassController
{
ISitecoreContext _iSitecoreContext;
public HomeBottomContentController() :this(new SitecoreContext()){
}
public HomeBottomContentController(ISitecoreContext iSitecoreContext)
{
_iSitecoreContext = iSitecoreContext;
}
public ActionResult IndexTest()
{
var model = _iSitecoreContext.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
}
[TestClass]
public class MvcUnitTests
{
[TestMethod]
public void Test_HomeBottomContentController_With_ISitecoreContext()
{
var model = new Home_Control()
{ Bottom_Content = "XYZ" };
var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>();
iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model);
HomeBottomContentController controllerUnderTest = new HomeBottomContentController(iSitecoreContext.Object);
var result = controllerUnderTest.IndexTest() as ViewResult;
}

write unit testing with mock in repository pattern

I have following interface and its repository class
Interface
public interface IIdentityRepository
{
bool CreateUser(ApplicationUser user, string password);
}
Repository
public class IdentityRepository : IIdentityRepository
{
ApplicationDbContext dbContext;
public IdentityRepository()
{
dbContext = new ApplicationDbContext(); // if none supplied
}
public bool CreateUser(ApplicationUser user, string password)
{
var userManager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(dbContext));
var idResult = userManager.Create(user, password);
return idResult.Succeeded;
}
}
public class UserManager : UserManager<ApplicationUser>
{
public UserManager()
: base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
}
}
This is the test class I'm trying to write for CreateUser method
In this method I'm using AppplicationUser as my model.
[TestClass]
public class IdentityRepositoryTest
{
private IdentityRepository _identitityRepo;
private Mock<IIdentityRepository> _identitityRepository;
private List<ApplicationUser> _users;
// initialize the test class
[TestInitialize]
public void TestSetup()
{
_identitityRepository = new Mock<IIdentityRepository>();
_users = new List<ApplicationUser>();
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>())).Callback<ApplicationUser>(c => _users.CreateUser(c));
_identitityRepo = new IdentityRepository();
}
#region Users
// check valid number of user/s(1) existing in current DB
[TestMethod]
public void IsValidtNumberofUsersExist()
{
// Arrange
_users.Add(new ApplicationUser { UserName = "Kez" , Email = "kez#gmail.com" });
// Act
var result = _identitityRepo.GetAllUsers();
Assert.IsNotNull(result);
// Assert
var numberOfRecords = result.ToList().Count;
Assert.AreEqual(1, numberOfRecords);
}
#endregion
}
But in here I'm having following compile time error
EDIT :
Once I change above error line as follows error went away.
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password")).Callba‌​ck<ApplicationUser>(‌​c => _users.Add(c));
but when I run this test, I'm getting following error
Result Message: Initialization method
ProjectName.UnitTest.Common.IdentityRepositoryTest.TestSetup threw
exception. System.ArgumentException: System.ArgumentException: Invalid
callback. Setup on method with parameters (ApplicationUser,String)
cannot invoke callback with parameters (ApplicationUser)..
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
.Callba‌​ck<ApplicationUser>(‌​c => _users.Add(c));
Should be:
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
.Callba‌​ck<ApplicationUser, string>(‌​(c, s) => _users.Add(c));
https://github.com/Moq/moq4/wiki/Quickstart#callbacks

Owin hosted webapi 2.2. testing controller with mocked service

I have webapi which for testing purposes I am hosting in owin. I have set it up using autofac. now when I am testing I want to inject moq dependencies. which I am not able to so far. I have read the documentation and did bit of research but I am missing something.
here is the testing code.
[Test]
public void Request_all_airports()
{
const int port = 8086;
AirportCollection moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
using (WebApp.Start<Startup>("http://localhost:" + port))
{
using (var mock = AutoMock.GetLoose())
{
var moqObj = mock.Mock<IAirportService>().Setup(x => x.GetAirports()).Returns(moqAirportCollection);
var client = new HttpClient {BaseAddress = new Uri("http://localhost:" + port)};
var response = client.GetAsync("/api/airport/get").Result;
var body = response.Content.ReadAsStringAsync().Result;
var airportCollection = JsonConvert.DeserializeObject<AirportCollection>(body);
}
}
}
Please have a look. let me know what I am missing. if you want to look at controller code or any other piece do let me know .
here is code for startup
public class Startup
{
public static IContainer container { get; set; }
public void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
container = AutofacSetup.Register(httpConfig);
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Thanks
I think so I have solved it with help from people. here is my code.
var moq = new Mock<IAirportService>();
moq.Setup(x => x.GetAirports()).Returns(moqAirportCollection);
newBuilder.RegisterInstance(moq.Object).As<IAirportService>();
newBuilder.Update(Startup.container);
I havnt rebuild the contrain i just updated it. autofac have behavior to use latest registration so it will use mocked on here.
You are almost there.
In your test you need to register your mock service with your autofac container so that dependencies on IAirportService are resolved with the mock in the application.
One way to achieve this is override the Startup class' Configuration method for each test and put your test DI in there. I've put some comments below to show changes that can be made:
public class Startup
{
public static IContainer container { get; set; }
// make this virtual
public virtual void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
// have this return the ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Then in your test class, derive from the Startup class and put your test logic in. Something like this:
public class MyTestCase {
public static Mock<IAirportService> MockObj { get; set; }
private class TestStartup : Startup {
public override void Configuration(IAppBuilder app) {
var httpConfig = new HttpConfiguration();
// this now returns ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
// register your mock, change this to whatever lifetime scope you need
var moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
var mock = AutoMock.GetLoose()
MockObj = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var moqObj = MockObj.Object;
builder.RegisterInstance(moqObj).As<IAirportService>();
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
[Test]
public void Request_all_airports()
{
using (var server = WebApp.Start<Startup>())
{
var response =
server.CreateRequest("/api/airport/get")
.GetAsync()
.Result;
var body = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<AirportCollection>(body);
// assert something
}
}
}
A unit test should test a single component. In your case, you are trying to test the AirportController through a HTTP query, not the AirportController as a standalone component.
The AirportController class depends on a IAirportService component. In order to test the component without any dependency you created a moq on IAirportService. Now you can instantiate a new AirportController with this moq and run your test using this instance.
If you have a AirportController like this
public class AirportController
{
public AirportController(IAirportService airportService) { /* ... */}
}
The AirportController test should be like this :
[Test]
public void Request_all_airports()
{
AirportCollection moqAirportCollection = new AirportCollection();
var moqAirPort = new Airport{ IATA = "moq",Name = "moqName" };
moqAirportCollection.Airports = new List<Airport>{ moqAirPort };
using (var mock = AutoMock.GetLoose())
{
var moqAirportService = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var testedAirportController = new AirportController(moqAirportService);
AirportCollection airportCollection = testedAirportController.Get();
Assert.AreEquals(1, airportCollection.Length, "Invalid number of airport");
Assert.AreEquals(moqAirPort.Name, airportCollection[0].Name, "Invalid name");
}
}

Mocked repository returning object with null properties

Here's a sample of one of my unit test classes (pared down to the basics). In the controller, when the Index() action method is invoked, a call to GetByID(1234) always results in a newed up instance of a Ticket object. The object exists, but all of its properties are null, even though I've set them in my fake object. Any ideas as to why?
I'm using Moq.
Unit test
[TestClass]
public class TicketControllerTests : ControllerTestBase
{
protected Mock<ITicketRepository> MockTicketRepository = new Mock<ITicketRepository>();
[TestMethod]
public void IndexActionModelIsTypeOfTicketModel()
{
//ARRANGE
Mock<HttpContextBase> context = FakeHttpContext();
context.Setup(ctx => ctx.Session[SessionKeys.TokenData.ToString()]).Returns(Constants.TOKENDATA_SUBMITTER);
MockTicketRepository.Setup(x => x.GetById(It.IsAny<int>())).Returns(Constants.CLIENT_TICKET);
//ACT
var result = GetController(context.Object).Index(Constants.TICKET_ID);
var model = ((ViewResult)result).Model;
//ASSERT
Assert.IsInstanceOfType(model, typeof(TicketModel), "ViewModel should have been an instance of TicketModel.");
}
private TicketController GetController(HttpContextBase context)
{
var controller = new TicketController(MockTicketRepository.Object);
controller.ControllerContext = GetControllerContext(context, controller);
return controller;
}
}
Constants.CLIENT_TICKET
public static Ticket CLIENT_TICKET
{
get
{
var ticket = new Ticket
{
CategoryID = 1,
CreatedByUserId = 4
};
ticket.Clients.Add(new Client { ShortName = "Test Client 1"});
ticket.Clients.Add(new Client { ShortName = "Test Client 2" });
ticket.User = new User {FirstName = "First", LastName = "Last"};
return ticket;
}
}
Controller
private readonly ITicketRepository _ticketRepository;
public TicketController(ITicketRepository ticketRepository)
{
_ticketRepository = ticketRepository;
}
public ActionResult Index(int id)
{
var ticket = _ticketRepository.GetById(id);
// etc...
}
Could you show the controller code under test? It could be related to how you have set up the mocked context but it's hard to tell without seeing the controller code.
Also, if you add MockBehavior.Strict when you create the mock, it will bomb out if the invocation doesn't have a corresponding expectation:
protected Mock<ITicketRepository> MockTicketRepository = new Mock<ITicketRepository>(MockBehavior.Strict);
UPDATE
I've tried to strip everything back so that the test is as simple as possible to try and isolate the issue. Here's what I have come up with:
[TestClass]
public class TicketControllerTests : ControllerTestBase
{
protected Mock<ITicketRepository> MockTicketRepository;
[TestMethod]
public void IndexActionModelIsTypeOfTicketModel()
{
//ARRANGE
MockTicketRepository = new Mock<ITicketRepository>(MockBehavior.Strict);
MockTicketRepository.Setup(x => x.GetById(Constants.TICKET_ID)).Returns(Constants.CLIENT_TICKET);
var controller = new TicketController(MockTicketRepository.Object);
//ACT - try to keep ACT as lean as possible, ideally just the method call you're testing
var result = controller.Index(Constants.TICKET_ID);
//ASSERT
var model = ((ViewResult)result).ViewData.Model;
Assert.That(model, Is.InstanceOfType<TicketModel>(), "ViewModel should have been an instance of TicketModel.")
}
}

Categories

Resources