如何从我的服务中测试 ClassifyComment() 方法。我有那个测试代码:
[TestClass]
public class SpamServiceTest
{
[TestMethod]
public void ClassifyCommentTest()
{
var spamComments = Builder<Comments>.CreateListOfSize(10).All().With(x => x.Content = "spam spam spam")
.Build().AsQueryable();
var mocker = new AutoMoqer();
mocker.GetMock<IUnitOfWork>()
.Setup(x => x.CommentsRepository.GetComments(It.Is<bool>(y => y == true)))
.Returns(spamComments);
//.......
}
}
但这给了我错误:无法实例化类的代理:CommentsRepository。找不到无参数构造函数。
下面是我要测试的代码:
public class SpamService : ISpamService
{
private readonly IUnitOfWork _unitOfWork;
public SpamService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public bool ClassifyComment(Comments comment)
{
var spam = _unitOfWork.CommentsRepository.GetComments(true).ToList();
//.............
}
}
public class UnitOfWork : IUnitOfWork
{
private DatabaseContext context = new DatabaseContext();
private CommentsRepository commentsRepository;
public CommentsRepository CommentsRepository
{
get
{
if (this.commentsRepository == null)
{
this.commentsRepository = new CommentsRepository(context);
}
return commentsRepository;
}
}
}
public class CommentsRepository : ICommentsRepository
{
private DatabaseContext context;
public CommentsRepository(DatabaseContext context)
{
this.context = context;
}
public virtual IQueryable<Comments> GetComments(bool isSpam)
{
//.......
}
}