1

I want to make unit tests for my project using a fake context (i'm currently using moq for that).

I have the following classes:


EpisodiosService.cs

public class EpisodiosService : IService<Episodio>
{
    private Context _context;

    public EpisodiosService(Context context = null)
    {
        if (context == null)
        {
            context = new Context();
        }
        _context = context;
    }

    ...
}

TesteHelper.cs

public class TesteHelper
{
    public static List<Episodio> lstEpisodios { get; set; }
    public static Mock<Context> mockContext { get; set; }

    public static Mock<Context> GerarMassaDeDados()
    {
        ...

        var mockSetEpisodio = new Mock<DbSet<Episodio>>();
        mockSetEpisodio.As<IQueryable<Episodio>>().Setup(m => m.Provider).Returns(lstEpisodios.AsQueryable().Provider);
        mockSetEpisodio.As<IQueryable<Episodio>>().Setup(m => m.Expression).Returns(lstEpisodios.AsQueryable().Expression);
        mockSetEpisodio.As<IQueryable<Episodio>>().Setup(m => m.ElementType).Returns(lstEpisodios.AsQueryable().ElementType);
        mockSetEpisodio.As<IQueryable<Episodio>>().Setup(m => m.GetEnumerator()).Returns(lstEpisodios.AsQueryable().GetEnumerator());

        mockContext = new Mock<Context>();
        mockContext.Setup(x => x.Episodio).Returns(mockSetEpisodio.Object);

        EpisodiosService episodiosService = new EpisodiosService(mockContext.Object);

        return mockContext;
    }

Episodio.cs

public class Episodio : ModelBase
{
    ...

    public Episodio()
    {
        nIdEstadoEpisodio = Enums.EstadoEpisodio.Ignorado;
        lstIntEpisodios = new List<int>();
        lstIntEpisodiosAbsolutos = new List<int>();
    }

    public bool IdentificarEpisodio()
    {
        ...

        EpisodiosService episodiosService = new EpisodiosService();
        List<Episodio> lstEpisodios = episodiosService.GetLista(oSerie);

        ...
    }

So, if in the test method i put some code like var service = new EpisodiosService(TesteHelper.GerarMassaDeDados()) and work with this service i would get the mocked content as intended, but there are some methods inside the some entities that consumes the service and i cannot pass the mocked context like at the Episodio.IdentificarEpisodio(), and if i create an instance of Episodio and call IdentificarEpisodio(), it will not use the mocked context because it isn't passed.

Is there a way to make the service use the mocked context without changing its signature (to IdentificarEpisodio(Context context) for exemple)?

I didn't want to change it's signature because there are a lot of methods that have this same problem and that i would have to change, and i don't think it would be nice to change it all...

Thanks in advance.

1 Answer 1

-1

To my opinion best way to solve that issue will be usage of dependency injection (you can use ninject or any other lib for this). Then you will be able to configure what context to use in any case.

If you using ninject easier solution will be create interface IContext and pass it as parameter in to service constructors like:

public class EpisodiosService : IService<Episodio>
{
    private Context _context;

    public EpisodiosService(Context context)
    {
        _context = context;
    }
    ...
}

Next step is to configure injection core, where you can set what implementation to use for each interface in constructor parameters of class, that will be injected.

For development project:

   var kernel = new StandardKernel();
   kernel.Bind<IContext>().To<Context>();

For unit tests:

   var kernel = new StandardKernel();
   kernel.Bind<IContext>().ToMethod(e => TesteHelper.GerarMassaDeDados());

Then you can get your services using this core:

var service = kernel.Get<EpisodiosService>();

In this way you will have required context for each case.

Please note that there are much more options to configure injection, for example you could inject in public properties marked with InjectAttribute or create more complex and general binding rules.

As easier solution you can just create some method CreateContext() that will return required type of context depending on some settings and use it in all your methods. For example:

Context CreateContext()
{
    if (isTest)
        return TesteHelper.GerarMassaDeDados();
    return new Context();
}

But this solution is less flexible than dependency injection.

3
  • But isn't it a bad practice to reference the unit test project at the main project? i've separated it in two different solutions, the first containing just the app itself (the main solution) and the other containing the unit tests and referencing the first solution. In this case the i'd have to add a reference to the test suite in the main solution too... About dependancy injection, i already have heard of it, but never searched about what it is... With it i'd have to reference the test solution too in the main solution? Dec 17, 2015 at 4:18
  • it's not a good idea to reference test project in main project. In that case you definitely should use dependency injection. I will add more explanation in my answer Dec 17, 2015 at 9:56
  • Thanks, you pointed me to the right direction. I learned about dependency injection and used Autofac to implement it, now it's working fine. Dec 18, 2015 at 2:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.