第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在測試方法中模擬按鍵?

如何在測試方法中模擬按鍵?

C#
呼喚遠方 2022-12-24 09:49:23
我寫了一個生命游戲控制臺應(yīng)用程序,現(xiàn)在我正在為它編寫單元測試。游戲板以循環(huán)方式呈現(xiàn),可以使用 Esc 鍵中斷。但是我不知道如何在我的主應(yīng)用程序類的測試方法中模擬該按鍵,所以我的測試當前無限循環(huán)。應(yīng)用.cspublic class Application{    private readonly IGame _game;    private readonly IBoard _board;    private readonly IBoardGenerator _boardGenerator;    private readonly IConsole _console;    public Application(IGame game, IBoard board, IBoardGenerator boardGenerator, IConsole console)    {        _game = game;        _board = board;        _boardGenerator = boardGenerator;        _console = console;    }    public void Run()    {        void RenderBoard()        {            _console.Clear();            _board.Evolve();            _board.Print();            Thread.Sleep(150);        }         LoopUntilButtonPressed(() =>        {             _console.Clear();            _game.NewGame();            _game.SetBoard(_board, _boardGenerator);            LoopUntilButtonPressed(RenderBoard, ConsoleKey.Escape);        }, ConsoleKey.Escape);    }    private void LoopUntilButtonPressed(Action action, ConsoleKey consoleKey)    {        do        {            while (!_console.KeyAvailable)            {                action.Invoke();            }        } while (_console.ReadKey(true) != consoleKey);    }
查看完整描述

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);            

}


查看完整回答
反對 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號