【问题标题】:How to test that the list Count is a certain value?如何测试列表 Count 是否为某个值?
【发布时间】:2016-11-02 02:45:32
【问题描述】:

我正在尝试通过查看有多少结果传递到我的Save method 来测试我的一种方法。相关行是:

await paymentSampleRepository.Received()
            .SaveSamplesAsync(Arg.Do<List<PaymentSamplePopulation>>(x => 
                                  Assert.Equal(sampleCount, x.Count())
                              ), modifiedBy);

我显然遗漏了一些关于如何测试这个的东西......我如何测试传递给SaveSamplesAsyncCount

这总是显示为通过。我在Assert 中尝试过sampleCountsampleCount + 1,它们都显示为通过!

如果需要,我可以展示整个测试方法。

【问题讨论】:

标签: c# unit-testing asp.net-core xunit.net nsubstitute


【解决方案1】:

参考Actions with argument matchers

假设跟随

public interface ILoader {
    Task LoadAsync(List<int> data);
}

public class SystemUnderTest {
    private readonly ILoader loader;
    public SystemUnderTest(ILoader loader) {
        this.loader = loader;
    }

    public async Task InvokeAsync(int count) {
        var data = Enumerable.Range(1,count).ToList();
        await loader.LoadAsync(data);
    }

}

测试看起来像这样

//Arrange
var expected = 2;
var actual = -1;

var loader = Substitute.For<ILoader>();
loader.LoadAsync(Arg.Do<List<int>>(x => actual = x.Count);

var sut = new SystemUnderTest(loader);

//Act
await sut.InvokeAsync(expected);

//Assert
Assert.Equal(expected, actual);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多