Mocking IDependencyResolver med Moq (ASP.NET MVC 3)
Jeg vil gerne teste en factoryklasse som benytter DependencyResolver til at oprette objektet.Jeg har flg. struktur i min factory:
{
private IDependencyResolver _dr;
private readonly IPersonRepository _repository;
public PersonFactory(IPersonRepository repository)
{
_repository = repository;
}
public IDependencyResolver Resolver
{
get { return _dr ?? DependencyResolver.Current; }
set { _dr = value; }
}
public IPerson CreateInstance()
{
return Resolver.GetService<IPerson>();
}
public IPerson GetByEmail(string email)
{
IPerson person = CreateInstance();
person.Email = email;
_repository.Get(person);
return person;
}
}
Nu vil jeg gerne teste min factory og det forsøger at gøre således:
public void GetByEmail()
{
// Arrange
var mock = new Mock<IPersonRepository>();
mock.Setup(r => r.Get(It.IsAny<IPerson>()));
var mockObj = new Mock<IPerson>();
var mockResolver = new Mock<IDependencyResolver>();
mockResolver.Setup(m => m.GetService<IPerson>()).Returns(mockObj.Object);
PersonFactory target = new PersonFactory(mock.Object);
target.Resolver = mockResolver.Object;
string email = "test@dummy.dk";
// Act
IPerson actual = target.GetByEmail(email);
// Assert
Assert.IsInstanceOfType(actual, typeof(IProfile));
}
Problemet er pt. at linjen med fed i testmetoden ikke fungerer. Jeg får en runtimefejl
Spørgsmålet er så hvad jeg skal skrive i stedet for, for at få det til at fungere?