Lee*_*eil 2 c# tdd rhino-mocks mocking
刚开始使用Rhino Mocks并且我有一个非常简单的问题,我如何使用设置属性的void来模拟一个类?
class SomeClass : ISomeClass
{
private bool _someArg;
public bool SomeProp { get; set; }
public SomeClass(bool someArg)
{
_someArg = someArg;
}
public void SomeMethod()
{
//do some file,wcf, db operation here with _someArg
SomeProp = true/false;
}
}
Run Code Online (Sandbox Code Playgroud)
显然这是一个非常人为的例子,谢谢.
在您的示例中,您不需要RhinoMocks,因为您显然正在测试被测试类的功能.简单的单元测试将改为:
[Test]
public void SomeTest()
{
var sc = new SomeClass();
// Instantiate SomeClass as sc object
sc.SomeMethod();
// Call SomeMethod in the sc object.
Assert.That(sc.SomeProp, Is.True );
// Assert that the property is true...
// or change to Is.False if that's what you're after...
}
Run Code Online (Sandbox Code Playgroud)
当你有一个依赖于其他类的类时,测试mocks会更有趣.在你的例子中,你提到:
//使用_someArg执行一些文件,wcf,db操作
也就是说你期望其他一些类设置SomeClass属性,这对mocktest更有意义.例:
public class MyClass {
ISomeClass _sc;
public MyClass(ISomeClass sc) {
_sc = sc;
}
public MyMethod() {
sc.SomeProp = true;
}
}
Run Code Online (Sandbox Code Playgroud)
所需的测试将是这样的:
[Test]
public void MyMethod_ShouldSetSomeClassPropToTrue()
{
MockRepository mocks = new MockRepository();
ISomeClass someClass = mocks.StrictMock<ISomeClass>();
MyClass classUnderTest = new MyClass(someClass);
someClass.SomeProp = true;
LastCall.IgnoreArguments();
// Expect the property be set with true.
mocks.ReplayAll();
classUndertest.MyMethod();
// Run the method under test.
mocks.VerifyAll();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7674 次 |
| 最近记录: |