Hi all!
I have a code contract on some interface IImageRepository:
[ContractClassFor(typeof(IImageRepository))]
sealed class IImageRepositoryContract : IImageRepository
{
IImage IImageRepository.GetById(int imageId)
{
Contract.Requires(imageId > 0);
return default(IImage);
}
}
I want to test code contracts some like that (specialy wrong DoesNotThrow):
[Fact]
public void IImageRepositoryContractTest()
{
IImageRepository mockImageRepository = new Mock<IImageRepository>().Object;
Assert.DoesNotThrow(() => mockImageRepository.GetById(-1));
}
but code contracts does not works in this case. And exception does not rise and test does not fail as expected. (I uncheck "Assert on Failure" in project properties).
The only solution I found - create fake class:
public class ImageRepositoryFake : IImageRepository
{
public IImage GetById(int imageId)
{
return default(IImage);
}
}
and after test runs like I want
[Fact]
public void IImageRepositoryContractTest()
{
IImageRepository mockImageRepository = new ImageRepositoryFake();
Assert.DoesNotThrow(() => mockImageRepository.GetById(-1));
}
Test fail with contract exception. How I can use mocking instead fake class?
PS: Sorry for my English