gpt4 book ai didi

c# - Visual Studio 2012 Fakes - 如何验证调用的方法?

转载 作者:太空狗 更新时间:2023-10-29 21:45:54 26 4
gpt4 key购买 nike

从使用 Moq 开始,我习惯于将模拟设置为可验证。如您所知,当您想要确保被测代码实际调用依赖项上的方法时,这会很方便。

例如最小起订量:

// Set up the Moq mock to be verified
mockDependency.Setup(x => x.SomethingImportantToKnow()).Verifiable("Darn, this did not get called.");
target = new ClassUnderTest(mockDependency);
// Act on the object under test, using the mock dependency
target.DoThingsThatShouldUseTheDependency();
// Verify the mock was called.
mockDependency.Verify();

我一直在使用 VS2012 的“Fakes Framework”(因为不知道它的更好名称),它非常灵活,我开始更喜欢它而不是 Moq,因为它似乎更具表现力并且可以制作 Shims简单的。但是,我不知道如何重现类似于 Moq 的可验证/验证实现的行为。我在 Stubs 上找到了 InstanceObserver 属性,这听起来可能是我想要的,但是截至 2012 年 9 月 4 日没有文档,而且我不清楚如何使用它,即使它是正确的。

任何人都可以为我指出正确的方向来做诸如 Moq Verifiable/Verify with VS2012's Fakes 之类的事情吗?

-- 2012 年 9 月 5 日编辑 --
我意识到了问题的解决方案,但我仍然想知道是否有内置的方法可以使用 VS2012 Fakes 来解决这个问题。如果可以的话,我会把这个打开一会儿,让别人来声明。这是我的基本想法(如果无法编译,我们深表歉意)。

[TestClass]
public class ClassUnderTestTests
{
private class Arrangements
{
public ClassUnderTest Target;
public bool SomethingImportantToKnowWasCalled = false; // Create a flag!
public Arrangements()
{
var mockDependency = new Fakes.StubIDependency // Fakes sweetness.
{
SomethingImportantToKnow = () => { SomethingImportantToKnowWasCalled = true; } // Set the flag!
}
Target = new ClassUnderTest(mockDependency);
}
}

[TestMethod]
public void DoThingThatShouldUseTheDependency_Condition_Result()
{
// arrange
var arrangements = new Arrangements();
// act
arrangements.Target.DoThingThatShouldUseTheDependency();
// assert
Assert.IsTrue(arrangements.SomethingImportantToKnowWasCalled); // Voila!
}
}

-- 2012 年 9 月 5 日结束编辑 --

最佳答案

由于我没有听说过更好的解决方案,我将 2012 年 9 月 5 日的修改称为目前最好的方法。

编辑找到描述最佳实践的神奇文章。 http://www.peterprovost.org/blog/2012/11/29/visual-studio-2012-fakes-part-3/

关于c# - Visual Studio 2012 Fakes - 如何验证调用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com