我希望能夠對我的假存儲庫(使用列表) 和我的真實存儲庫(使用數據庫)運行測試以確保兩者我的嘲笑版本按預期工作,我的實際生產存儲庫按預期工作。我認爲最簡單的方法是使用TestCase我可以使用NUnit TestCase測試嘲諷的存儲庫和真實存儲庫
private readonly StandardKernel _kernel = new StandardKernel();
private readonly IPersonRepository fakePersonRepository;
private readonly IPersonRepository realPersonRepository;
[Inject]
public PersonRepositoryTests()
{
realPersonRepository = _kernel.Get<IPersonRepository>();
_kernel = new StandardKernel(new TestModule());
fakePersonRepository = _kernel.Get<IPersonRepository>();
}
[TestCase(fakePersonRepository)]
[TestCase(realPersonRepository)]
public void CheckRepositoryIsEmptyOnStart(IPersonRepository personRepository)
{
if (personRepository == null)
{
throw new NullReferenceException("Person Repostory never Injected : is Null");
}
var records = personRepository.GetAllPeople();
Assert.AreEqual(0, records.Count());
}
但它要求一個常量表達式。