[英]How can I fake something which returns different answers over time using FakeItEasy?
我正在嘗試使用 FakeItEasy 偽造一個密封的外部音頻源。
我已經包裝了音頻源並成功地偽造了包裝器,所以我知道基礎知識是正確的。 這是我目前堅持的一點:
音頻源在調用 Play() 后返回 isPlaying = true。 isPlaying 將保持為 true,直到音頻剪輯播放完畢,此時它將返回 false。
我可以使用以下代碼偽造第一部分:
A.CallTo(() => fakeAudioSourceWrapper.Play())
.Invokes(() => fakeAudioSourceWrapper.isPlaying = true)
在調用 Play function 之后,成功偽造的是 Playing being true。 我想做的是延遲一定時間然后讓它返回 false 以模擬剪輯播放完畢后發生的情況。
我試過了
A.CallTo(() => fakeAudioSourceWrapper.Play())
.Invokes(() => fakeAudioSourceWrapper.isPlaying = true)
.Then
.Invokes(() => fakeAudioSourceWrapper.isPlaying = false);
想法是在調用 Play() 后第一次調用 isPlaying 時將返回 true,第二次返回 false,但沒有得到任何樂趣。
有沒有辦法做到這一點?
如果多次調用Play
,您的解決方案可能會起作用,因為您正在配置 Play 的功能(盡管我認為您切換了 true 和 false)。 我從這個問題中猜測Play
只被調用一次。 在這種情況下,您希望 Play 執行一個操作,該操作將在對 isPlaying 的調用之間設置更改行為。
考慮這個通過測試中的方法,其中Play
導致isPlaying
恰好返回true
一次(未配置的返回 bool 的 Fake 方法的默認行為是返回false
):
public interface IAudioSourceWrapper {
public bool isPlaying {get;set;}
public void Play();
}
[Fact]
public void PlayThenDoNotPlay()
{
var fakeAudioSourceWrapper = A.Fake<IAudioSourceWrapper>();
A.CallTo(() => fakeAudioSourceWrapper.Play())
.Invokes(() => {
A.CallTo(() => fakeAudioSourceWrapper.isPlaying).Returns(true).Once();
});
fakeAudioSourceWrapper.isPlaying.Should().Be(false);
fakeAudioSourceWrapper.Play();
fakeAudioSourceWrapper.isPlaying.Should().Be(true);
fakeAudioSourceWrapper.isPlaying.Should().Be(false);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.