模拟ImongoCollection驱动程序2.0.1 C#
我正在尝试为使用MongoDb 2.0.1驱动程序的存储库层编写单元测试 我对我的收藏有依赖性 有人知道如何嘲笑,避免使用真正的数据库? 多谢各位 代码如下:模拟ImongoCollection驱动程序2.0.1 C#,c#,mongodb,repository,moq,C#,Mongodb,Repository,Moq,我正在尝试为使用MongoDb 2.0.1驱动程序的存储库层编写单元测试 我对我的收藏有依赖性 有人知道如何嘲笑,避免使用真正的数据库? 多谢各位 代码如下: public class Repository<T> : IRepository<T> where T : Content { private readonly IMongoCollection<T> _mongoCollection; public Repository(IMo
public class Repository<T> : IRepository<T>
where T : Content
{
private readonly IMongoCollection<T> _mongoCollection;
public Repository(IMongoCollection<T> mongoCollection)
{
_mongoCollection = mongoCollection;
}
public void Insert(T entity)
{
_mongoCollection.InsertOneAsync(entity);
}
public async void Update(T entity)
{
await _mongoCollection.InsertOneAsync(entity);
}
public async void Delete(string id)
{
await _mongoCollection.DeleteOneAsync(Builders<T>.Filter.Where(x => x.Id == id));
}
public async Task<T> GetById(string id)
{
return await _mongoCollection.Find(Builders<T>.Filter.Where(x => x.Id == id)).FirstOrDefaultAsync();
}
/// <summary>
/// This method will retrieve a list of items
/// by passing a dynamic mongo query
/// Eg. AND - var filter = builder.Eq("cuisine", "Italian") & builder.Eq("address.zipcode", "10075");
/// Eg. OR - var filter = builder.Eq("cuisine", "Italian") | builder.Eq("address.zipcode", "10075");
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<List<T>> GetByFilter(FilterDefinition<T> filter)
{
filter = filter ?? new BsonDocument();
return await _mongoCollection.Find(filter).ToListAsync();
}
public async Task<List<T>> GetAll()
{
return await _mongoCollection.Find(new BsonDocument()).ToListAsync();
}
}
公共类存储库:IRepository
其中T:内容
{
私有只读IMongoCollection\u mongoCollection;
公共存储库(IMongoCollection mongoCollection)
{
_mongoCollection=mongoCollection;
}
公共无效插入(T实体)
{
_mongoCollection.InsertOneAsync(实体);
}
公共异步无效更新(T实体)
{
等待mongoCollection.InsertOneAsync(实体);
}
公共异步无效删除(字符串id)
{
wait _mongoCollection.DeleteOneAsync(Builders.Filter.Where(x=>x.Id==Id));
}
公共异步任务GetById(字符串id)
{
返回wait _mongoCollection.Find(Builders.Filter.Where(x=>x.Id==Id)).FirstOrDefaultAsync();
}
///
///此方法将检索项目列表
///通过传递一个动态mongo查询
///例如AND-var filter=builder.Eq(“烹饪”、“意大利语”)和builder.Eq(“address.zipcode”、“10075”);
///例如,OR-var filter=builder.Eq(“烹饪”、“意大利语”)| builder.Eq(“address.zipcode”、“10075”);
///
///
///
公共异步任务GetByFilter(FilterDefinition筛选器)
{
过滤器=过滤器??新的B文档();
return wait_mongoCollection.Find(filter.toListSync();
}
公共异步任务GetAll()
{
return wait _mongoCollection.Find(new BsonDocument()).toListSync();
}
}
我在尝试类似的事情
private Mock<IMongoCollection<Content>> _mockContentCollection;
private IRepository<Content> _contentRepository;
[SetUp]
public void TestMethod1()
{
_mockContentCollection = new Mock<IMongoCollection<Content>>();
_mockContentCollection.Setup(x => x.)....
_contentRepository = new Repository<Content>(_mockContentCollection.Object);
}
[Test]
public void GetAll_With_Results()
{
var a = _contentRepository.GetAll();
}
private Mock\u mockContentCollection;
私人IRepository内容存储库;
[设置]
公共void TestMethod1()
{
_mockContentCollection=新建Mock();
_mockContentCollection.Setup(x=>x.)。。。。
_contentRepository=新存储库(\u mockContentCollection.Object);
}
[测试]
public void GetAll_与_结果()
{
var a=_contentRepository.GetAll();
}
到目前为止您尝试了什么?问题到底出在哪里?我认为为存储库编写单元测试并模拟存储库的关键部分毫无意义。他正在寻求一种使用monogdb C#driver(在SQL Server中,我们可以播种)复制数据的方法,因此他正在寻找类似@LzyPanda的选项