第二个方法调用的单元测试

发布于 2025-01-16 20:38:08 字数 1331 浏览 1 评论 0原文

我有一个单元测试,其中使用 Moq 和 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();
    }
}

我想检查我的方法的第二次调用。 不幸的是,当我调试此代码时, spff.GetSymbols() 方法仅调用一次,并且在 result.Should().Equals(expectedResult) 行上调用检查结果。这里有某种延迟加载 - 仅当需要结果时才调用该方法。 为什么不在 spff.GetSymbols() 行中立即调用它?如何改变这种行为以及如何在单元测试中调用测试方法两次?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2025-01-23 20:38:08

感谢@Dennis Doomen,问题已得到解决。

关于不立即执行方法的问题是在 spff.GetSymbols() 方法的实现中,该方法使用 yield return 因此它与单元测试并不真正相关。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文