1 回答

TA貢獻1830條經(jīng)驗 獲得超3個贊
在控制臺抽象上模擬ReadKey和成員。KeyAvailable
確保Setup發(fā)生在被測方法之前。在這種情況下是Run. 這樣,模擬將在調(diào)用時按預(yù)期運行。
我還建議您設(shè)置一個序列,KeyAvailable以便在while.
[Test]
public void Run_MethodCalled_GameCorrectlySet() {
//Arrange
_fakeConsole.Setup(_ => _.ReadKey(It.IsAny<bool>())).Returns(ConsoleKey.Escape);
_fakeConsole.SetupSequence(_ => _.KeyAvailable)
.Returns(false) // will be returned on 1st invocation
.Returns(true); // will be returned on 2nd invocation to break while
//Act
_application.Run();
//Assert
_fakeGame.Verify(_ => _.NewGame(), Times.Once);
}
- 1 回答
- 0 關(guān)注
- 108 瀏覽
添加回答
舉報