Whi*_*uit 15 c# autofixture nsubstitute
目前我正在使用EF6在UnitOfWork中实现我的存储库.我还创建了一个In-Memory模拟实现(MockUnitOfWork和MockRepository),以便我可以在单元测试中使用它们,但是我现在必须处理对象的繁琐设置.
这不是Autofixture的设计目的吗?我如何获得可以在包含Foo和Barr存储库的测试中使用的MockUnitOfWork ?我正在使用NSubstitute作为我的模拟框架.
IUnitOfWork
public interface IUnitOfWork
{
void Save();
void Commit();
void Rollback();
IRepository<Foo> FooRepository { get; }
IRepository<Bar> BarRepository { get; }
}
Run Code Online (Sandbox Code Playgroud)
IRepository
public interface IRepository<TEntity> where TEntity : class
{
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null);
TEntity GetByID(object id);
void Insert(TEntity entity);
void Delete(object id);
void Delete(TEntity entityToDelete);
void Update(TEntity entityToUpdate);
}
Run Code Online (Sandbox Code Playgroud)
是的,这正是它的设计目的。请参阅下面的示例。我使用 Mock 而不是 NSubstitute,因为我不熟悉 NSubstitute。您只需要传递另一个自定义,并在设置中使用 NSubstitute 语法。
[SetUp]
public void SetUp()
{
// this will make AutoFixture create mocks automatically for all dependencies
_fixture = new Fixture()
.Customize(new AutoMoqCustomization());
// whenever AutoFixture needs IUnitOfWork it will use the same mock object
// (something like a singleton scope in IOC container)
_fixture.Freeze<Mock<IUnitOfWork>>();
// suppose YourSystemUnderTest takes IUnitOfWork as dependency,
// it'll get the one frozen the line above
_sut = _fixture.Create<YourSystemUnderTest>();
}
[Test]
public void SomeTest()
{
var id = _fixture.Create<object>(); // some random id
var fooObject = _fixture.Create<Foo>(); // the object repository should return for id
// setuping THE SAME mock object that wa passed to _sut in SetUp.
// _fixture.Freeze<Mock part is ESSENTIAL
// _fixture.Freeze<Mock<IUnitOfWork>>() returns the mock object, so whatever comes
// next is Mock specific and you'll have to use NSubstitute syntax instead
_fixture.Freeze<Mock<IUnitOfWork>>()
.Setup(uow => uow.FooRepository.GetById(id))
.Returns(fooObject);
// if this method will ask the unit of work for FooRepository.GetById(id)
// it will get fooObject.
var whatever = _sut.SomeMethod(id);
// do assertions
}
Run Code Online (Sandbox Code Playgroud)
AutoFixture 的美妙之处在于,您不必为被测系统的所有依赖项创建模拟。如果您正在测试仅使用一个依赖项的功能,则只需在创建被测系统之前冻结它即可。其余的依赖项将被自动模拟。
| 归档时间: |
|
| 查看次数: |
2010 次 |
| 最近记录: |