我有一个DispatcherTimer,我检查该定时器的计时器滴答中组件的忙/闲状态。我必须等到组件变得空闲,像IsBusy()方法返回false,然后我必须自动启动一些东西。我想通过首先模拟组件忙于测试场景,然后在一段时间后让组件自由并看到自动功能启动。当然,一旦我调用被测代码,我就进入等待状态。是否有可能从测试中设定新的期望并发送更新到生产代码,以便我可以做我需要做的事情?我正在使用Nunit进行单元测试。通过单一方法调用对犀牛嘲笑的期望
2
A
回答
1
可以使用犀牛嘲笑Do() Handler
模拟预先指定的等待时间在组件的IsBusy()
方法的嘲笑:
[TestFixture]
public class TestClass
{
[Test]
public void MyTest()
{
var mocks = new MockRepository();
var mockComponent = mocks.DynamicMock<MyComponent>();
using (mocks.Record())
{
Expect.Call(() => mockComponent.IsBusy())
.Do((Func<bool>)(() =>
{
System.Threading.Thread.Sleep(10000); // wait 10 seconds
return false;
}));
// perhaps define other expectations or asserts here...
}
using (mocks.Playback())
{
var classUnderTest = new ClassUnderTest(mockComponent);
classUnderTest.MethodUnderTest();
}
mocks.VerifyAll();
}
}
然后,可以测试不同的睡眠时间经由多个单元测试根据需要或使用NUnit's Parameterized Tests(I只是任意选择了等待10秒)。
的ClassUnderTest.MethodUnderTest()
应在其执行某些点直接或间接也许通过你所提到的DispatcherTimer
的Tick
事件处理程序调用MyComponent.IsBusy()
。没有看到你的代码,我的猜测是,你可能有一些与此类似:
public class ClassUnderTest
{
private MyComponent myComponent;
public ClassUnderTest(MyComponent myComponent)
{
this.myComponent = myComponent;
}
public void MethodUnderTest()
{
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();
// ...
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
if(!myComponent.IsBusy())
{
// do something else now...
}
}
}
public class MyComponent
{
public virtual bool IsBusy()
{
// some implementation that will be faked via the Do Handler
return false;
}
}
0
您的期望可以动态创建,但应该设置在一个地方,而不是“交互式”。在执行代码测试过程中,您不应该尝试更改它们。
为了实现你的目标,你可以尝试使用Repeat
选项允许检查,以循环一定次数:
mock.Expect(theMock => theMock.IsBusy())
.Return(true)
.Repeat.Times(5);
mock.Expect(theMock => theMock.IsBusy())
.Return(false);
相关问题
- 1. 用犀牛嘲笑嘲笑系列
- 2. 犀牛嘲笑:嘲讽HttpRequestBase.Files
- 3. 使用犀牛嘲笑为嘲讽一个WebClient的方法
- 4. 嘲笑HttpPostedFileBase与犀牛嘲笑
- 5. 犀牛嘲笑的AssertWasNotCalled
- 6. 干燥与犀牛嘲笑
- 7. 能在犀牛嘲笑不是素数的方法调用
- 8. 犀牛让我嘲笑基于类型的通用方法吗?
- 9. 犀牛嘲笑期望没有正确返回集合
- 10. 犀牛嘲笑异常期望#1当前#0:需要帮助
- 11. 犀牛嘲笑 - 断言方法调用给定参数
- 12. 犀牛与单身人士嘲笑
- 13. 犀牛模拟,从非嘲笑对象
- 14. 嘲讽FirstOrDefault在犀牛嘲笑
- 15. 犀牛嘲笑一个电话
- 16. 验证对象调用自身的犀牛嘲笑
- 17. 如何嘲笑说TAKS谓语用犀牛制品的方法
- 18. 单元测试通用仓库与犀牛嘲笑
- 19. 犀牛嘲笑存根和嘲笑只适用于接口?
- 20. 使用犀牛嘲笑嘲笑OUT参数,这是我测试
- 21. asp.net的MVC犀牛嘲笑嘲笑的HttpRequest值
- 22. 犀牛嘲笑存根VS期望,总是选择第一个为什么?
- 23. 犀牛嘲笑访问数据库
- 24. 犀牛嘲笑,AssertWasCalled与Arg限制
- 25. 犀牛嘲笑会话状态
- 26. 犀牛嘲笑,无效和性能
- 27. 犀牛嘲笑分步教程
- 28. 注入犀牛嘲笑“假”工厂
- 29. 犀牛嘲笑:存根&嘲笑。有什么区别
- 30. 犀牛嘲笑和无序调用的有序测试
我没有得到机会使用Rhino.Mocks非常频繁,所以请纠正我,如果我错了:)希望这个例子仍然适用,tho。 –