特别是在我的情况下,如果 Foo 是一个属性 IBar,并且我已经嘲笑了 Foo。
if (!(Foo is Bar bar))
{
Logger.Error("ERROR, NOT CORRECT TYPE");
return false;
}
如果可能的话,我想让我的Mock<IBar>不Mock<Bar>。
完整的未片段示例:
Bar 类,什么 Foo 属性被“强制转换”为:
public class Bar : IBar
{
// Stuff I don' care about because I am using a mock
}
测试类:
[TestClass]
public class InuEmulatorCustomBehaviourBaseTests
{
Mock<IBar> _IBarMock;
[TestInitialize]
public void TestInitialise()
{
_IBarMock= new Mock<IBar>();
// Code used to set up a spy to check log messages produced during each test
}
[TestMethod]
public void UnitUnderTest_Initialise_LogsNoErrors_when_Foo_is_of_type_Bar_Test()
{
//Arrange
var unitUnderTest = new UnitUnderTest { Foo = _IBarMock.Object };
//Act
unitUnderTest.Initialise();
//Assert
Assert.AreEqual(0, _spiedLogMessage.Count, "An error was logged when none should have been.");
}
[TestCleanup]
public void TestCleanup()
{
// Code used to reset the spy to check log messages produced during each test
}
}
和 UnitUnderTestClass:
public class UnitUnderTest
{
IBar _foo;
public IBar Foo
{
private get
{
return _foo ?? _foo = new Bar();
}
set { _foo = value; } //set as new Mock<IBar> during unit test
}
public bool Initialise()
{
if (!(Foo is Bar bar))
{
Logger.Error("ERROR, NOT CORRECT TYPE");
return false; //don't want my test to follow this path
}
return true; //want my test to follow this path
}
}
我明白为什么上述测试失败,我想知道我是否可以以及如何在不使用Mock<Bar>.
呼啦一阵风
相关分类