【发布时间】:2016-02-13 17:35:22
【问题描述】:
我有一个 MongoDB 的通用存储库。
这是我的 Get 方法:
public IList<TEntity> Get<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> filter = null) where TEntity : class, new()
{
var collection = GetCollection<TEntity>();
var query = Query<TEntity>.Where(filter);
var entity = collection.FindAs<TEntity>(query).ToList();
return entity;
}
当我尝试模拟它时,我得到一个错误:
IList<Login>(其中 Login 是我的业务对象)不包含任何 ReturnsForAnyArgs 的定义。
[TestMethod]
public void CheckIfUserNameExits_IfUserNameDoesNotExist_ReturnFalse()
{
Login login = null;
Task<IList<Login>> logl = null;
// _mongoDAL.Get<Arg.Any<Login>()>(Arg.Any<Expression<Func<TEntity, bool>>>).ReturnsForAnyArgs
//_mongoDAL.When(x => x.Get<Login>(Arg.Any<Expression<Func<Login, bool>>>())).ReturnsForAnyArgs(logl);
_mongoDAL.Get<Login>(Arg.Any<Expression<Func<Login, bool>>>()).ReturnsForAnyArgs(logl);
}
关于如何模拟它的任何建议,以便我可以在单元测试中设置我想要的返回值?
【问题讨论】:
-
"...不包含 ReturnsForAnyArgs 的任何定义" -- 这听起来像您缺少
using NSubstitute;? -
是的,我把它包括在内。 :)
标签: unit-testing generics nsubstitute