[英]Moq concrete class without using virtual keyword
我這里有以下代碼
ITest.cs
public interface ITest
{
int Prop { get; }
int Calc();
}
測試.cs
public class Test : ITest
{
public int Prop { get; private set; }
public int Calc()
{
return Prop * 2;
}
}
我想測試Calc()
方法。 如果我的理解是正確的,您不能在不使用virtual
關鍵字的情況下覆蓋具體類的 getter 屬性。
例如
var moq = new Mock<Test>(); // mocked Test class
moq.Setup(x => x.Prop).Returns(2); // throws an error,
// however with "virtual int Prop { get; }" it doesn't throw any exceptions
var moq = new Mock<ITest>(); // mocked ITest interface
moq.Setup(x => x.Prop).Returns(2); // works fine
// but can not really test the Calc() method (it returns 0
// because the proxy doesn't have Test's implementation)
所以我的問題是:如何在不制作virtual int Prop
的情況下測試Calc()
的功能
順便說一句...這也不起作用
var moq = new Mock<ITest>();
moq.Setup(x => x.Prop).Returns(2);
var obj = (Test)moq.Object; // can NOT convert ITestProxy to Test
如果你想測試Calc()
然后測試Calc()
。 你不需要嘲笑任何東西
[TestMethod]
public void Test_Calc_DoublesProp()
{
//arrange
var test = new Test(5); //assumes Prop value can be injected in constructor
//act
int result = test.Calc();
//assert
Assert.IsTrue(result == 10); // 5 * 2
}
找到解決方案:姿勢
它使用 ILGenerator 並且很舊,但它可以工作。
例子
var test = new Test(); // default value of 'Prop' is 0
var shim = Shim.Replace(() => test.Prop).With((Test @this) => // idk why he needs @this param
{
return 100; // sets the 'Prop' value to 100
});
var result = 0;
PoseContext.Isolate(() =>
{
result = test.Calc(); // prints 200;
}, shim);
模擬將被用來替代您嘗試測試的依賴項。
關於您的測試,只需測試 class 的實際暴露行為,這意味着不要嘗試使用私有方法和設置器。 在任何情況下,模擬 class 進行測試都不是有效的方法。
例如:
Test
class:[TestClass]
public class TestTests
{
[TestMethod]
public void Calc_WhenPropIsSetTo5_Returns10()
{
/**
* Seems this is what you want to test (see name of methods),
* and I don't think it is a good idea, as your setter is private.
* Since it's not a "public" behavior, you could either
*
* a) have the setter as internal for testing purposes,
*
* b) (my recommendation) go for the two other tests below instead;
* this will actually test the exposed behavior of the class.
*
* c) circumvent the accessibility (by using a library or using
* Reflection directly) to set the value of the prop.
* See your own answer for an example.
*/
}
[TestMethod]
public void Calc_WhenCreatedWith5_Returns10()
{
// Arrange
var testSubject = new Test(5); // if the prop is set by constructor... otherwise arrange in the proper way.
// Act
var actualResult = testSubject.Calc();
// Assert
Assert.AreEqual(expected: 10, actualResult)
}
[TestMethod]
public void Prop_WhenCreatedWith5_IsSetTo5()
{
// Arrange
var testSubject = new Test(5);
// Act
var actualResult = testSubject.Prop;
// Assert
Assert.AreEqual(expected: 5, actualResult)
}
}
ITest.Calc()
的東西,那么你應該在你的實際代碼中,
1 -取決於接口ITest
,而不是實現Test
2 - 將依賴注入到正在測試的 class 中,這樣您就可以用Mock<ITest>.Object()
替換通常的實現Test
public class ClassThatUsesTest
{
private readonly ITest _test;
public ClassThatUsesTest(ITest test)
{
_test = test;
}
public int SomeFunction()
{
if (_test.Calc() == 10)
{
return 42;
}
else
{
return 0;
}
}
}
[TestClass]
public class ClassThatUsesTestTests
{
[TestMethod]
public void SomeFunction_WhenTestCalcReturns10_Returns42()
{
// Arrange
var testMock = new Mock<ITest>();
testMock.Setup(x => x.Calc()).Returns(10);
var testSubject = new ClassThatUsesTest(testMock.Object);
var expectedResult = 42;
// Act
var actualResult = testSubject.SomeFunction();
//
Assert.AreEqual(expectedResult, actualResult);
}
}
在最后一個示例中,您可以看到模擬幫助我們隔離測試,而不依賴於Test
class 的實際實現細節。 即使突然間實際的Test
被破壞或改變(例如,你需要通過 6 而不是 5 來獲得 10),測試方法並不關心。
雖然這感覺不對,因為它為了測試目的而改變了方法實現,只是為了分享:
public class Test : ITest
{
private int _prop;
public int Prop
{
get => ((ITest)this).Prop;
private set => _prop = value;
}
public int Calc()
{
return Prop * 2;
}
int ITest.Prop => _prop;
}
在測試中:
var mock = new Mock<Test>();
mock.As<ITest>()
.Setup(x => x.Prop)
.Returns(3);
// This should return 6
mock.Object.Calc();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.