Unit test IList bullying method

I am trying to make fun of the following TryGetApns method :

    private readonly Func<string, ICommunicationClient> _communicationFactory;

    public CommunicationApiFacade(Func<string, ICommunicationClient> communicationFactory)
    {
        _communicationFactory = communicationFactory;
    }

    public IList<ApnResponse> TryGetApns(string tenant)
    {
        GetApnsResponse response = null;
        try
        {
            var communicationApiClient = _communicationFactory(tenant);
            response = communicationApiClient.JctConfigurationService.GetApns();
        }
        catch (HttpException e)
        {
            ...
        }

        return response?.Apns ?? new List<ApnResponse>();
    }

with the following test:

    private Mock<ICommunicationApiFacade> _communicationApiFacade;

    [SetUp]
    public void SetUp()
    {
        _fixture = new Fixture()
            .Customize(new AutoMoqCustomization());

        _communicationApiFacade = _fixture.Freeze<Mock<ICommunicationApiFacade>>();
    }

    [Test]
    public void RunJctTests_WhenJctIsInAPrivateNetwork_ShouldReturnAPassedTest()
    {
        // Arrange
        var jctApn = _fixture.Create<string>();
        var message = _fixture.Build<JctLoggedIn>()
            .With(x => x.ApnFromDevice, jctApn)
            .Create();

        var response = _fixture.Build<ApnResponse>()
            .With(x => x.IsPrivateApn, true)
            .With(x => x.ApnName, jctApn).Create();

        _communicationApiFacade.Setup(x => x.TryGetApns(string.Empty))
            .Returns(new List<ApnResponse> { response });

        var subject = _fixture.Create<NetworkProviderTestRunner>();

        // Act
        var result = subject.Execute(message);

        // Assert
        Assert.That(result.Result, Is.True);
    }

and this is the NetworkProviderTestRunner class :

    private readonly ICommunicationApiFacade _communicationApi;

    public NetworkProviderTestRunner(ICommunicationApiFacade communicationApi)
    {
        _communicationApi = communicationApi;
    }

    public JctTest Execute(JctLoggedIn message)
    {
        var apns = _communicationApi.TryGetApns(message.Tenant);

        var jctApn = apns.FirstOrDefault(x => x.ApnName == message.ApnFromDevice);

        if (jctApn != null)
        {
            var privateApn = apns.FirstOrDefault(x => x.PublicApnId.Equals(jctApn.Id));
            if (privateApn != null || jctApn.IsPrivateApn)
                return new JctTest { Result = true };
        }
        return new JctTest { Result = false };
    }

JctLoggedIn Class :

public class JctLoggedIn : Message
{
    public string Imei { get; set; }
    public string SimCardIdFromDevice { get; set; }
    public string SimCardIdFromStorage { get; set; }
    public string ApnFromDevice { get; set; }
    public string ApnFromStorage { get; set; }
    public string FirmwareFromDevice { get; set; }
    public int DeviceTypeFromStorage { get; set; }
    public int SerialNumber { get; set; }
}

but for some reason I always return an empty list. I tried to populate the list in SetUp, and also define the output there, but I am always the same. Any help?

+4
source share
3 answers

While you can explicitly omit the property Tenantwhen creating the object message, you can also change the Mock setting to this:

_communicationApiFacade.Setup(x => x.TryGetApns(message.Tenant))
    .Returns(new List<ApnResponse> { response });
+3
source

This line

_communicationApiFacade.Setup(x => x.TryGetApns(string.Empty))
        .Returns(new List<ApnResponse> { response });

, , TryGetApns .

message.Tenant :

var message = _fixture.Build<JctLoggedIn>()
        .With(x => x.ApnFromDevice, jctApn)
        .Create();

string.empty, .

0

Replace

var response = _fixture.Build<ApnResponse>()
            .With(x => x.IsPrivateApn, true)
            .With(x => x.ApnName, jctApn).Create();

with

var response = _fixture.Build<ApnResponse>()
            .With(x => x.Tenant, String.Empty)
            .With(x => x.IsPrivateApn, true)
            .With(x => x.ApnName, jctApn).Create();

if the Tenant is available for recording.

If the property is writable, AutoFixture will create an object with a non-empty value for Tenant without explicitly setting what you want.

0
source

Source: https://habr.com/ru/post/1671048/


All Articles