简体   繁体   English

为什么我的 xunit 测试结果中会出现 System.ArgumentException?

[英]Why do I get System.ArgumentException in my xunit test result?

Here is the method I want to write test for:这是我要为其编写测试的方法:

public class JobStore : IJobStore
{
    private readonly IMongoDbContext _context;

    public JobStore(IMongoDbContext context)
    {
        _context = context;
    }



    public async Task<IJob> CreateAsync(IJob job)
    {
        await _context.Jobs.InsertOneAsync(job as Job);
        return job;
    }

}

Here is my test:这是我的测试:

public class JobStoreTest
{
    

    private readonly Mock<IMongoDbContext> _moqIMongoDbContext;
    private readonly JobStore _jobStore;

    public JobStoreTest()
    {
        _moqIMongoDbContext = new Mock<IMongoDbContext>();
        _jobStore = new JobStore(_moqIMongoDbContext.Object);


        _moqIMongoDbContext
            .Setup(_ => _.Jobs.InsertOneAsync(It.IsAny<Job>(), It.IsAny<CancellationToken>()))
            .Returns((IJob x) => Task.FromResult(x));
    }

    

    
    [Theory]
    [ClassData(typeof(JobClassesForTesting))]
    public async Task CreateAsync(IJob job)
    {
        var result = await _jobStore.CreateAsync(job);

        Assert.Equal(job,result as Job);
    }
}

Here is the result of test:以下是测试结果:

System.ArgumentException Invalid callback. System.ArgumentException 无效回调。 Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1).具有 2 个参数的方法上的设置无法调用具有不同数量参数 (1) 的回调。

Here is the JobClassesForTestingClass which is my scenario for testing:这是我的测试场景 JobClassesForTestingClass:

public class JobClassesForTesting : IEnumerable<object[]>
{
    public IEnumerator<object[]> GetEnumerator()
    {
        yield return new object[]
        {
                new Job()
                {
                    Payload = null,
                    EntityName = "EntityNameTest1",
                    Module = "ModuleTest1",
                    MetaData = new Dictionary<string, string>(),
                    Categories = new List<JobCategory>{new JobCategory {Name = "CategoryTest1"} },
                    PublishedDate = DateTime.Now
                }
        };

        yield return new object[]
        {
                new Job()
                {
                    Payload = "PayloadTest2",
                    EntityName = "EntityNameTest2",
                    Module = "ModuleTest2",
                    MetaData = new Dictionary<string, string>(),
                    Categories = new List<JobCategory>{new JobCategory {Name = "CategoryTest2"} },
                    PublishedDate = DateTime.Now
                }
        };

        yield return new object[]
        {
                new Job()
                {
                    Payload = "PayloadTest3",
                    EntityName = "EntityNameTest3",
                    Module = "ModuleTest3",
                    MetaData = new Dictionary<string, string>(),
                    Categories = new List<JobCategory>{new JobCategory {Name = "CategoryTest3"} },
                    PublishedDate = DateTime.Now
                }
        };
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

I want the result of the test would be as same as each of my Job objects but the result of the test is System.ArgumentException我希望测试的结果与我的每个 Job 对象相同,但测试的结果是 System.ArgumentException

The exception clearly states the problem about the arguments count mismatch.该异常清楚地说明了有关 arguments 计数不匹配的问题。

Refactor the mock setup重构模拟设置

  _moqIMongoDbContext
        .Setup(_ => _.Jobs.InsertOneAsync(
            It.IsAny<Job>(), 
            It.IsAny<InsertOneOptions>(),
            It.IsAny<CancellationToken>())
        )
        .Returns((Job document, InsertOneOptions options, CancellationToken token) 
            => Task.FromResult(document));

Note how the parameters in the Returns delegate/callback now matches the number and types of argument matchers used by the member that was setup.请注意Returns委托/回调中的参数现在如何匹配已设置成员使用的参数匹配器的数量和类型。

IMongoCollection.InsertOneAsync Method (TDocument, InsertOneOptions, CancellationToken) IMongoCollection.InsertOneAsync 方法(TDocument、InsertOneOptions、CancellationToken)

Task InsertOneAsync(
    TDocument document,
    InsertOneOptions options = null,
    CancellationToken cancellationToken = null
)

Reference Moq Quickstart to get a better understanding of how to use that mocking library参考Moq Quickstart以更好地了解如何使用 mocking 库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在List上调用Sort(IComparer)时会出现System.ArgumentException? - Why do I get a System.ArgumentException when invoking Sort(IComparer) on a List? 为什么抛出类型为&#39;System.ArgumentException&#39;的异常 - why threw an exception of type 'System.ArgumentException' System.ArgumentException:'targetBounds' - System.ArgumentException: 'targetBounds' 发生System.ArgumentException - System.ArgumentException occurred 异常“System.ArgumentException” - exception 'System.ArgumentException' 运行 C# Selenium 测试时获取 System.ArgumentException - Getting System.ArgumentException when running C# Selenium Test EF核心异常System.ArgumentException:参数类型与System.Linq.Expressions.Expression.Condition(表达式测试,表达式…)不匹配 - EF core exception System.ArgumentException:Argument types do not match System.Linq.Expressions.Expression.Condition(Expression test, Expression …) 存在System.ArgumentException的捕获块,但是无论如何都没有捕获到异常,为什么? - Catch block for System.ArgumentException exists, but exception is not caught anyway, why? 为什么在添加成员时BindingList抛出System.ArgumentException - Why does BindingList throw a System.ArgumentException when adding a member WindowPhone FlipView System.ArgumentException - WindowPhone FlipView System.ArgumentException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM