【发布时间】:2017-08-03 03:26:19
【问题描述】:
我有这个代码:
[TestMethod]
public void TestMethod()
{
TextBox txtBox= new TextBox() { Text = "Test" };
PrivateObject privateObj= new PrivateObject(someObject);
var mockObj = new Mock<PrivateObject>();
mockObj.Setup(x => x.Invoke("SomeMethod", It.IsAny<string>())).Returns(true);
object result = privateObj.Invoke("DoSomething", txtBox, EventArgs.Empty);
Assert.AreEqual(txtBox.Text, string.Empty);
}
“DoSomething”方法调用“SomeMethod”方法,该方法返回一个布尔类型。我如何模拟 SometMetod,这样当我调用它时,他会返回我需要的东西?
【问题讨论】:
-
someObject中的new PrivateObject(someObject)是什么? -
看看这篇文章,看看它是否适合你的场景。 adamprescott.net/2012/08/28/unit-testing-and-private-methods
标签: c# unit-testing mocking moq privateobject.invoke