0

Problem

I am trying to test my service. when I run test it show this error and test failed. my unity config class is in my asp.net mvc project and my test is in different project I don't know where I am doing wrong.

unable to get default constructor for Class servicetest

ServiceTest

 [TestClass]
public class ImportServiceTests 
{
    private readonly IImportService _importService;
    private readonly IUnitOfWork _unitOfWork;

    public ImportServiceTests(IImportService importService, IUnitOfWork unitOfWork)
    {
        _importService = importService;
        _unitOfWork = unitOfWork;
    }

    [TestMethod]
    public void ImportCategories()
    {
        string filePath = Path.GetFullPath(@"E:\categories.xlsx");
        if (File.Exists(filePath))
        {
            Stream data = File.OpenRead(filePath);
            string fileName = Path.GetFileName(filePath);

            _importService.ImportCategoriesFromXlsx(data);
        }
    }
}

UnityConfig

    public class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    /// <summary>
    /// Gets the configured Unity container.
    /// </summary>
    public static IUnityContainer GetConfiguredContainer()
    {
        return container.Value;
    }
    #endregion

    /// <summary>Registers the type mappings with the Unity container.</summary>
    /// <param name="container">The unity container to configure.</param>
    /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
    /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
    public static void RegisterTypes(IUnityContainer container)
    {
        // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
        // container.LoadConfiguration();

        // TODO: Register your types here
        // container.RegisterType<IProductRepository, ProductRepository>();
        container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
        container.RegisterType<IImportService, ImportService>(new PerRequestLifetimeManager());
        container.RegisterType<IDataContext, PriceHunterDataContext>(new PerRequestLifetimeManager());
    }
}
3
  • You can't use Dependancy injection like that in unit tests. Your composition root doesn't run when running tests. You will need to mock IImportService and IUnitOfWork in a method decorated with [TestInitialize] (call this method TestSetup or something) and remove your constructor. Commented Feb 1, 2017 at 6:06
  • @garethb I am trying to create integration testing so what I can do in set method? Commented Feb 1, 2017 at 6:16
  • stackoverflow.com/questions/21263197/… Commented Feb 1, 2017 at 6:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.