1

I have a unit test where I am using Moq and Fluent Assertions:

[Fact]
public void GetSymbols_ShouldSetSucceedToTrue_WhenSecondAttemptSucceed()
{
    string selectedFileName = "testFileName.txt";
    string[] expectedResult = new string[] { "testSymbol1", "testSymbol2" };
    Mock<IOpenFileDialogService> mockFileDialogService = new Mock<IOpenFileDialogService>();
    mockFileDialogService.SetupSequence(m => m.ShowDialog()).Returns(false).Returns(true);
    mockFileDialogService.Setup(m => m.FileName).Returns(selectedFileName);
    Mock<IFileService> mockFileService = new Mock<IFileService>();
    mockFileService.Setup(m => m.ReadAllLines(selectedFileName)).Returns(expectedResult);
    SymbolsProviderFromFile spff = new SymbolsProviderFromFile(mockFileDialogService.Object, mockFileService.Object);

    // Act
    spff.GetSymbols();
    IEnumerable<string> result = spff.GetSymbols();

    // Assert
    using (new AssertionScope())
    {
        result.Should().Equal(expectedResult);
        spff.Succeed.Should().BeTrue();
    }
}

I would like to check the second call of my method. Unfortunately when I debug this code, the spff.GetSymbols() method is only called once and it is called on the result.Should().Equals(expectedResult) line when the result is checked. There is some kind of a lazy loading here - the method is only called when the result is needed. Why is it not called immediately in the spff.GetSymbols() line? How can I change this behavior and how can I call testing method twice in unit test?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Qwertyluk
  • 363
  • 3
  • 14

1 Answers1

0

Problem has been resolved thanks to @Dennis Doomen.

An issue about not executing method immediately was in the implementation of the spff.GetSymbols() method which uses yield return so it wasn't really related to unit testing.

Qwertyluk
  • 363
  • 3
  • 14