【发布时间】:2010-12-10 01:32:38
【问题描述】:
我是使用 Moq 的新手,我找不到这样做的方法。 我有一个 generateId 私有方法,叫做
/// <summary>
/// Generates a call Id for external interfaces
/// </summary>
/// <returns></returns>
private string GenerateCallId()
{
return "EX" + SharedServicesClientProxy.Instance.GenerateId().ToString();
}
我想对这个方法进行单元测试,因此我需要模拟代理。 SharedServicesClientProxy 只是一个实现接口 ISharedServices 但添加了单例的对象。我想测试所有字符串是否正确返回了以“EX”开头的字符串。这是我的单元测试,使用最小起订量
/// <summary>
/// A test for GenerateCallId
/// A CallId for external systems should always start by "EX"
///</summary>
[TestMethod()]
[DeploymentItem("myDll.dll")]
public void GenerateCallIdTest()
{
myService_Accessor target = new myService_Accessor();
var SharedServicesClientProxy = new Mock<ISharedServices>();
SharedServicesClientProxy.Setup(x => x.GenerateId()).Returns(5396760556432785286);
string actual;
string extCallIdPrefix = "EX";
actual = target.GenerateCallId();
Assert.IsTrue(actual.StartsWith(extCallIdPrefix));
}
我猜我是在错误的地方进行模拟?
以更一般的方式,我如何模拟将由我正在测试的方法调用的对象? 例如:
/// <summary>
/// dummy test
///</summary>
[TestMethod()]
[DeploymentItem("myDll.dll")]
public void Foo()
{
myService_Accessor target = new myService_Accessor();
boolexpected = false;
actual = target.Foo();
Assert.IsTrue(actual,expected);
}
/// <summary>
/// Foo
/// </summary>
/// <returns></returns>
private bool Foo()
{
return this.Bar();
}
我需要起订量吧,但我需要在哪里做呢? 谢谢, 赛博
【问题讨论】:
标签: c# unit-testing moq