以下のインターフェースで:
public interface IRepository<T>
{
T FirstOrDefault(Func<T, bool> predicate);
}
次のように実装します。
private IRepository<Foo> Repository {get;set;}
public FooService(IRepository<Foo> repository)
{
Repository = repository;
}
public void Foo(int bar)
{
var result = Repository.FirstOrDefault(x => x.Id.Equals(bar));
if (result == null)
{
throw new Exception("Returned null, not what I expected!");
}
}
次のようなテストを書くと:
repos = MockRepository.GenerateMock<IRepository<Foo>>(null);
repos.Expect(x => x.FirstOrDefault(y => y.Id.Equals(Arg<int>.Is.Anything))).Throw(new Exception("This exception should be thrown!"));
FooService f = new FooService(repos);
f.Foo(1);
//expecting exception to be thrown
予想される例外がスローされないため、モックされた呼び出しが無視されている/呼び出されていないと推測しています。
ただし、追加IgnoreArguments()
すると呼び出され、予想される例外が発生します。
repos.Expect(x => x.FirstOrDefault(y => y.Id.Equals(Arg<int>.Is.Anything))).IgnoreArguments().Throw(new Exception("This exception should be thrown!"));
私が間違っていることについて何か考えはありますか?