我有一個MongoDB的通用存儲庫。Nsubstitute設置通用存儲庫的返回值
這是我的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>
(其中登錄名是我的業務對象)不包含任何對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定義」 - 這聽起來像是你缺少一個'使用NSubstitute;'? –
是的,我已經包括它。 :) – Strawberry