为什么我的 xunit 测试结果中出现 System.ArgumentException?
Why do I get System.ArgumentException in my xunit test result?
这是我要为其编写测试的方法:
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;
}
}
这是我的测试:
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);
}
}
测试结果如下:
System.ArgumentException
回调无效。具有 2 个参数的方法设置无法调用具有不同数量参数 (1) 的回调。
这是我的测试场景 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();
}
我希望测试结果与我的每个 Job 对象相同,但测试结果是 System.ArgumentException
异常清楚地说明了参数计数不匹配的问题。
重构模拟设置
_moqIMongoDbContext
.Setup(_ => _.Jobs.InsertOneAsync(
It.IsAny<Job>(),
It.IsAny<InsertOneOptions>(),
It.IsAny<CancellationToken>())
)
.Returns((Job document, InsertOneOptions options, CancellationToken token)
=> Task.FromResult(document));
请注意 Returns
delegate/callback 中的参数现在如何匹配已设置成员使用的参数匹配器的数量和类型。
IMongoCollection.InsertOneAsync Method (TDocument, InsertOneOptions, CancellationToken)
Task InsertOneAsync(
TDocument document,
InsertOneOptions options = null,
CancellationToken cancellationToken = null
)
参考 Moq Quickstart 以更好地了解如何使用该模拟库
这是我要为其编写测试的方法:
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;
}
}
这是我的测试:
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);
}
}
测试结果如下:
System.ArgumentException 回调无效。具有 2 个参数的方法设置无法调用具有不同数量参数 (1) 的回调。
这是我的测试场景 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();
}
我希望测试结果与我的每个 Job 对象相同,但测试结果是 System.ArgumentException
异常清楚地说明了参数计数不匹配的问题。
重构模拟设置
_moqIMongoDbContext
.Setup(_ => _.Jobs.InsertOneAsync(
It.IsAny<Job>(),
It.IsAny<InsertOneOptions>(),
It.IsAny<CancellationToken>())
)
.Returns((Job document, InsertOneOptions options, CancellationToken token)
=> Task.FromResult(document));
请注意 Returns
delegate/callback 中的参数现在如何匹配已设置成员使用的参数匹配器的数量和类型。
IMongoCollection.InsertOneAsync Method (TDocument, InsertOneOptions, CancellationToken)
Task InsertOneAsync(
TDocument document,
InsertOneOptions options = null,
CancellationToken cancellationToken = null
)
参考 Moq Quickstart 以更好地了解如何使用该模拟库