현재 TDD를 사용하여 클래스를 만들고 있습니다. 클래스는 특정 윈도우가 활성화 될 때까지 기다린 다음 몇 가지 메소드를 실행하는 작업을 담당합니다.Rhino Mock으로 블로킹 호출 조롱하기
내가 원하는 동작이 실제로 AutoIt의 단일 메서드이기 때문에 AutoIt COM 라이브러리 (AutoIt에 대한 자세한 내용은 here을 참조하십시오)를 사용하고 있습니다.
코드는 다음과 같이 거의이다
public class WindowMonitor
{
private readonly IAutoItX3 _autoItLib;
public WindowMonitor(IAutoItX3 autoItLib)
{
_autoItLib = autoItLib;
}
public void Run() // indefinitely
{
while(true)
{
_autoItLib.WinWaitActive("Open File", "", 0);
// Do stuff now that the window named "Open File" is finally active.
}
}
}
당신은 AutoIt이 COM 라이브러리 내가 (NUnit과와 코뿔소 모의 객체를 사용) 조롱 수있는 인터페이스 느릅 나무 구현 볼 수 있듯이 :
[TestFixture]
public class When_running_the_monitor
{
WindowMonitor subject;
IAutoItX3 mockAutoItLibrary;
AutoResetEvent continueWinWaitActive;
AutoResetEvent winWaitActiveIsCalled;
[SetUp]
public void Setup()
{
// Arrange
mockAutoItLibrary = MockRepository.GenerateStub<IAutoItX3>();
mockAutoItLib.Stub(m => m.WinWaitActive("", "", 0))
.IgnoreArguments()
.Do((Func<string, string, int, int>) ((a, b, c) =>
{
winWaitActiveIsCalled.Set();
continueWinWaitActive.WaitOne();
return 1;
}));
subject = new Subject(mockAutoItLibrary)
// Act
new Thread(new ThreadStart(subject.Run)).Start();
winWaitActiveIsCalled.WaitOne();
}
// Assert
[Test]
[Timeout(1000)]
public void should_call_winWaitActive()
{
mockAutoItLib.AssertWasCalled(m => m.WinWaitActive("Bestand selecteren", "", 0));
}
[Test]
[Timeout(1000)]
public void ensure_that_nothing_is_done_while_window_is_not_active_yet()
{
// When you do an "AssertWasCalled" for the actions when the window becomes active, put an equivalent "AssertWasNotCalled" here.
}
을 }
문제는 첫 번째 테스트가 시간 초과를 유지한다는 것입니다. 필자는 이미 스텁 "WinWaitActive"가 호출 될 때 (의도 된대로 별도의 스레드에서) 차단하고, 그 후에 "AssertWasCalled"가 호출되면 실행이 반환되지 않는다는 것을 알았습니다.
진행 방법을 잃어 버렸고 차단 호출을 조롱하는 예를 찾을 수 없었습니다. 그래서 결론에
:
시험 시간 제한을하지 않고 차단 호출을 조롱하는 방법이 있나요?
(추신 : 여기서는 가능하지 않을 수 있으므로 디자인을 변경하는 데 덜 관심이 있습니다 (예 : "차단 호출을 사용하지 마십시오").하지만 훨씬 어려운 경우가 있습니다. 디자인을 변경하려면 좀 더 일반적인 솔루션에 관심이 있습니다. 그러나 차단 호출을 모의하기가 단순히 불가능한 경우 해당 제안은 더 환영합니다!)
실제로 테스트하고 싶은 것은 무엇입니까? 왜 모의를 차단해야합니까? 테스트에 멀티 스레딩이 필요한 이유는 무엇입니까? –