如何为此方法编写单元测试:
public void ClassifyComments()
{
IEnumerable<Comment> hamComments = _commentRepository.FindBy(x => x.IsSpam == false);
IEnumerable<Comment> spamComments = _commentRepository.FindBy(x => x.IsSpam == true);
//....
}
该FindBy方法将表达式作为参数:
public virtual IEnumerable<T> FindBy(Expression<Func<T, bool>> filter)
{
return dbSet.Where(filter).ToList();
}
到目前为止,这是我的单元测试:
IEnumerable<Comment> spamComments = Builder<Comment>.CreateListOfSize(10).All()
.With(x => x.Content = "spam spam spam")
.Build();
IEnumerable<Comment> hamComments = Builder<Comment>.CreateListOfSize(10).All()
.With(x => x.Content = "ham ham ham")
.Build();
var mockRepository = new Mock<IGenericRepository<Comment>>();
mockRepository
.Setup(x => x.FindBy(It.Is<Expression<Func<Comment, bool>>>(y => y.IsSpam == true)))
.Returns(spamComments);
mockRepository
.Setup(x => x.FindBy(It.Is<Expression<Func<Comment, bool>>>(y => y.IsSpam == true)))
.Returns(hamComments);
但是我无法编译它,如何更改此测试以便模拟生成hamCommentsand的值spamComments。
错误 2“System.Linq.Expressions.Expression>”不包含“IsSpam”的定义,并且找不到接受“System.Linq.Expressions.Expression>”类型的第一个参数的扩展方法“IsSpam”(你是缺少 using 指令或程序集引用?)