I am writing some tests using Moq Framework. The application is build upon MVC pattern. I came across an issue and I seem to be stuck there forever. I need to write test for Service which code looks more or less this way:
public class MyService : IService
{
private readonly IRepoInterface _inter1;
private readonly Interface2 _inter2;
private readonly Interface3 _inter3;
public MyService(
IRepoInterface inter1,
Interface2 inter2,
Interface3 inter3)
{
_inter1= inter1;
_inter2= inter2;
_inter3= inter3;
}
//there go methods
}
What I basically need to do is check if foreach element from the string list of names of methods which IRepoInterface gets from DB, MyService's method invokes it.
I tried writing a builder (and I figured I don't need it, but now I am not sure), I tried simply mocking IRepoInterface list returning method in the initialization of my tests:
List<string> methods = new List<string> { "method1", "method2" };
Mock.Get(IRepoInterface)
.Setup(x => x.GetListOfMethods())
.Returns(methods);
This does not work (no method is invoked, test fails).
I guess my mistake is that I do not recreate dependency within MyService, since I mock IService and IRepoInterface separately, but at this point, as a newbie, I am totally confused. If you have any suggestions I'd be very grateful.
Aucun commentaire:
Enregistrer un commentaire