How can I test ClassifyComment() method from my service. I have that test code:
[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);
//.......
}
}
But it gives me error: Can not instantiate proxy of class: CommentsRepository. Could not find a parameterless constructor.
Below is my code which I want test:
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)
{
//.......
}
}
IUnityOfWork
should return a ICommentsRepository
, ie an interface, not an implementation. The mock of IUnityOfWork should return a mock of ICommentsRepository.
Let the abstraction work with other abstractions, not with implementations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.