第二个方法调用的单元测试
我有一个单元测试,其中使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢@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 usesyield return
so it wasn't really related to unit testing.