I'using IIndex as factory to decide wich service to use. When I'm trying to unit test my CommunicationJob class I'm struggling with the mock of IIndex.
public class CommunicationJob : BaseJob
{
private readonly IRepo<Notification> _nr;
private readonly IIndex<string, IService> _cs;
public CommunicationJob
(
IRepo<Notification> nr,
IIndex<string, IService> cs
)
{
_nr= nr;
_cs= cs;
}
public void Do(DateTime date)
{
foreach (var n in _nr.GetList())
{
_cs[n.GetType().Name].Send(n);
nr.Sent = DateTime.Now;
nr.Update(n, true);
}
}
}
And the problem is that this is _cs[n.GetType().Name] null. Has anyone any solution to my problem? One solution could be to initiate Autofac before the test, but I don't know how to load AutoFac in the test context.
My test look like this:
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void WithNotifications(int numberOfNotifications)
{
var fixture = new TestCommunicationJobFixture();
var sut = fixture.WithNotifications(numberOfNotifications).GetSut();
sut.Do(new DateTime());
fixture.MockCommunicationService.Verify(x => x["EmailNotification"].Send(It.Is<Notification>(z => z.Sent != null)), Times.Exactly(numberOfNotifications));
fixture.MockNotificationRepo.Verify(x => x.Update(It.Is<Notification>(z => z.Sent != null), true), Times.Exactly(numberOfNotifications));
}
So I have recreated something similar to your setup
public class Something
{
private readonly IIndex<string, IService> index;
public Something(IIndex<string, IService> index)
{
this.index = index;
}
public void DoStuff()
{
this.index["someString"].Send();
}
}
public interface IIndex<TKey, TValue>
{
TValue this[TKey index] {get;set;}
}
public interface IService
{
void Send();
}
And then test like so (using Moq):
// Arrange
var serviceMock = new Mock<IService>();
var indexMock = new Mock<IIndex<string, IService>>();
indexMock.Setup(x => x[It.IsAny<string>()]).Returns(serviceMock.Object);
var something = new Something(indexMock.Object);
// Act
something.DoStuff();
// Assert
serviceMock.Verify(x => x.Send());
Hopefully this will point you in the right direction. Obviously you will need to Mock your IRepo<Notification>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With