Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟IList的单元测试方法_C#_Unit Testing_Autofixture - Fatal编程技术网

C# 模拟IList的单元测试方法

C# 模拟IList的单元测试方法,c#,unit-testing,autofixture,C#,Unit Testing,Autofixture,我试图模仿以下方法TryGetApns: private readonly Func<string, ICommunicationClient> _communicationFactory; public CommunicationApiFacade(Func<string, ICommunicationClient> communicationFactory) { _communicationFactory = communic

我试图模仿以下方法TryGetApns

    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>();
    }
JctLoggedIn等级:

    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 };
    }
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; }
}
但出于某种原因,我总是会得到一张空名单。我试图在设置中填充列表,并在其中定义输出,但我始终是一样的。有什么帮助吗?

这行

_communicationApiFacade.Setup(x => x.TryGetApns(string.Empty))
        .Returns(new List<ApnResponse> { response });
\u communicationapfacade.Setup(x=>x.TryGetApns(string.Empty))
.Returns(新列表{response});
当使用租户参数的空字符串调用TryGetAppns时,通知模拟返回包含响应的列表

通过此行创建消息后,message.Tenant的值是多少:

var message = _fixture.Build<JctLoggedIn>()
        .With(x => x.ApnFromDevice, jctApn)
        .Create();
var message=\u fixture.Build()
.With(x=>x.ApnFromDevice,jctApn)
.Create();
如果不是string.empty,您的模拟将不会返回响应列表。

Replace

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

var响应=_fixture.Build()
.With(x=>x.Tenant,String.Empty)
.With(x=>x.isprivatepn,true)
.With(x=>x.ApnName,jctApn).Create();
如果租户是可写的


如果属性是可写的,AutoFixture将为租户创建一个非空值的对象,而无需显式设置所需内容。

虽然在构建
消息
对象时可以显式省略
租户
属性,但也可以将模拟设置更改为:

_communicationApiFacade.Setup(x => x.TryGetApns(message.Tenant))
    .Returns(new List<ApnResponse> { response });
\u communicationapfacade.Setup(x=>x.TryGetApns(message.Tenant))
.Returns(新列表{response});

您可以发布NetworkProviderTestRunner的代码吗?根据您的设置,TryGetAppns将仅在租户为空字符串时返回您设置的值。值得检查的是,您是否按照设置传递了正确的值。已在@mani完成。请同时发布
JctLoggedIn
,以便我们可以查看
Tenant
是否可写。另一方面,您可以删除第一个代码块,因为您要用测试双精度替换所有代码块!填充后,测试成功了。感谢@MarkSeemann指出这一点并感谢大家这将导致它返回
null
是模拟处于
Loose
模式,或者在
Strict
模式下抛出异常。我认为
Tenant
JctLoggedIn
上的一个属性,而不是
ApnResponse
。当然,从手术中还不完全清楚。。。
var response = _fixture.Build<ApnResponse>()
            .With(x => x.Tenant, String.Empty)
            .With(x => x.IsPrivateApn, true)
            .With(x => x.ApnName, jctApn).Create();
_communicationApiFacade.Setup(x => x.TryGetApns(message.Tenant))
    .Returns(new List<ApnResponse> { response });