如何在构造函数C#xunit中设置量量值

发布于 2025-02-09 12:05:13 字数 2002 浏览 0 评论 0原文

我有一个函数,其中包含验证,该函数检查变量值是否为null。我想使用Xunit测试测试该类方法。但是由于此验证,我未能调用单位测试。

public interface ICountry<CountryModel>
    {
        Task ProcessCall(IList<string> countryCodes);
    }
    public class CallOptions
    {
        public string Name { get; set; }
    }
  
    public class Country<CountryModel> : ICountry<CountryModel>
    {
        
        private readonly CallOptions _options;
        private readonly Irepo _repo;

        public Country(IOptions<CountryModel> options,Irepo repo)
        {         _repo= repo
            _options = options.Value;

            if (string.IsNullOrWhiteSpace(_options.Name))
            {
                throw new ArgumentException("Missing  name");
            }
        }

        public async Task ProcessCall(IList<string> Codes)
        {
           _repo.set(Codes);
        }

单位测试,

public class ProcessorTests 
{
    private  Mock<Irepo> _RepositoryMock;
    private Mock<IOptions<CallOptions>> options;

    public ProcessorTests()
    {
        _RepositoryMock = new Mock<Irepo>();
        options = new Mock<IOptions<CallOptions>>();
        options.SetReturnsDefault("test");    

}

private Country<CountryModel> CreateSut()
    {
        return new Country<CountryModel>(_RepositoryMock.Object, _options.Object);
    }
    
  


    [Fact]
    public async Task ShouldCheck()
    {
        GetRepoMock();
        await CreateSut().ProcessCall(new string[] { "TEST" });

        _RepositoryMock.Verify(x => x.set(It.IsAny<List<string>>()), Times.Once);
    }
  private void GetRepoMock()
    {
        _RepositoryMock
            .Setup(m => m.set(It.IsAny<List<string>>())
            .ReturnsAsync(new Response<Code>(false, null, Enumerable.Empty<Code>()));
    }

但是当单元测试执行时, _options.name 的值是空的,并且在称为国家方法时未能进行测试。 对此有任何想法吗?

I have a function which contains validation that check whether a variable value is null or not. I want to test that class method using xunit test. But due to this validation, I failed to call the Unit Test.

Class

public interface ICountry<CountryModel>
    {
        Task ProcessCall(IList<string> countryCodes);
    }
    public class CallOptions
    {
        public string Name { get; set; }
    }
  
    public class Country<CountryModel> : ICountry<CountryModel>
    {
        
        private readonly CallOptions _options;
        private readonly Irepo _repo;

        public Country(IOptions<CountryModel> options,Irepo repo)
        {         _repo= repo
            _options = options.Value;

            if (string.IsNullOrWhiteSpace(_options.Name))
            {
                throw new ArgumentException("Missing  name");
            }
        }

        public async Task ProcessCall(IList<string> Codes)
        {
           _repo.set(Codes);
        }

Unit Test

public class ProcessorTests 
{
    private  Mock<Irepo> _RepositoryMock;
    private Mock<IOptions<CallOptions>> options;

    public ProcessorTests()
    {
        _RepositoryMock = new Mock<Irepo>();
        options = new Mock<IOptions<CallOptions>>();
        options.SetReturnsDefault("test");    

}

private Country<CountryModel> CreateSut()
    {
        return new Country<CountryModel>(_RepositoryMock.Object, _options.Object);
    }
    
  


    [Fact]
    public async Task ShouldCheck()
    {
        GetRepoMock();
        await CreateSut().ProcessCall(new string[] { "TEST" });

        _RepositoryMock.Verify(x => x.set(It.IsAny<List<string>>()), Times.Once);
    }
  private void GetRepoMock()
    {
        _RepositoryMock
            .Setup(m => m.set(It.IsAny<List<string>>())
            .ReturnsAsync(new Response<Code>(false, null, Enumerable.Empty<Code>()));
    }

But when the unit test executes, the value of _options.Name is empty and failed the test while called the Country method.
Any idea regarding this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文