これは動作するはずです:
A.CallTo(() => this.fakeRepo.Get(1))
.Throws<Exception>().Twice()
.Then
.Returns("a");
もう一つの方法は、シーケンスのようにそれを実行します。
var funcs = new Queue<Func<string>>(new Func<string>[]
{
() => throw new Exception(),
() => throw new Exception(),
() => "a",
});
A.CallTo(() => this.fakeRepo.Get(1)).ReturnsLazily(() => funcs.Dequeue().Invoke()).NumberOfTimes(queue.Count);
は、拡張メソッドを持ってもらえ:
public static IThenConfiguration<IReturnValueConfiguration<T>> ReturnsNextLazilyFromSequence<T>(
this IReturnValueConfiguration<T> configuration, params Func<T>[] valueProducers)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (valueProducers == null) throw new ArgumentNullException(nameof(valueProducers));
var queue = new Queue<Func<T>>(valueProducers);
return configuration.ReturnsLazily(x => queue.Dequeue().Invoke()).NumberOfTimes(queue.Count);
}
アンはこのようにそれを呼び出す:
A.CallTo(() => this.fakeRepo.Get(1)).ReturnsNextLazilyFromSequence(
() => throw new Exception(),
() => throw new Exception(),
() => "a");
または '().Twice()。Then.Returns(" a ")';) –