Question

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));
}
Was it helpful?

Solution

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>.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top