0

I am trying to test one of my methods by seeing how many results are being passed into my Save method. The pertinent line is:

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

I am obviously missing something about how to test this... how do I test the Count of what's being passed into SaveSamplesAsync

This is always showing as passing. I've tried sampleCount and sampleCount + 1 in the Assert and they both show as passing!

If need be, I can show the whole test method.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183

1 Answers1

1

Reference Actions with argument matchers

Assuming following

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);
    }

}

A test would look like this

//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);
Nkosi
  • 235,767
  • 35
  • 427
  • 472