[英]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.