TypeMock非常强大.我想它可以做到.对于其他模拟框架,如Moq或Rhino,您将需要使用其他策略.
Rhino或Moq策略:
每个示例:您正在使用Asssembly类来获取程序集的全名.
public class YourClass
{
public string GetFullName()
{
Assembly ass = Assembly.GetExecutingAssembly();
return ass.FullName;
}
}
Run Code Online (Sandbox Code Playgroud)
该大会从接口派生类_Assembly.因此,您可以注入接口,而不是直接使用Assembly.然后,很容易模拟测试的界面.
修改后的类:
public class YourClass
{
private _Assembly _assbl;
public YourClass(_Assembly assbl)
{
_assbl = assbl;
}
public string GetFullName()
{
return _assbl.FullName;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的测试中,你嘲笑_Assembly:
public void TestDoSomething()
{
var assbl = MockRepository.GenerateStub<_Assembly>();
YourClass yc = new YourClass(assbl);
string fullName = yc.GetFullName();
//Test conditions
}
Run Code Online (Sandbox Code Playgroud)