[英]Get single object from MOQ using Unit Testing, Unit of Work and Generic Repository Pattern framework
I'm new to Moq, currently, the problem is when I try to get a collection of the object (eg WebStore) its works as expected, but when I try to get a single object from it's not work as expected,我是 Moq 的新手,目前,问题是当我尝试获取对象的集合(例如 WebStore)时,它按预期工作,但是当我尝试从中获取单个对象时,却没有按预期工作,
could you please guide me on how to get a single object from the repository你能指导我如何从存储库中获取单个对象吗
private Mock<WebStore> _webstore;
[SetUp]
public void SetUp()
{
...
_unitOfWork = new Mock<IUnitOfWork>();
_webstore = new Mock<WebStore>();
...
}
public WebStore GetAllWebstore()
{
var webstoreDat = unitOfWork.GetRepository<WebStore>().GetAll();
return webstoreData;
}
public WebStore GetWebstorebyId(int webstoreId)
{
var webstoreData = unitOfWork.GetRepository<WebStore>()
.Get(store => store.WebStoreId == webstoreId).FirstOrDefault();
return webstoreData;
}
[Test]
public void GetWebstore_All()
{
//
var webStoreId = 1;
var customerName = "A";
var listWebstore = new List<WebStore>() { new WebStore { WebStoreId = webStoreId, CustomerName = customerName } };
var webstore = new WebStore { WebStoreId = webStoreId, CustomerName = customerName };
//Set up Mock
_unitOfWork.Setup(uow => uow.GetRepository<WebStore>().GetAll()).Returns(listWebstore); // working
...
}
[Test]
public void GetWebstore_GetSingle()
{
//
var webStoreId = 1;
var customerName = "A";
var listWebstore = new List<WebStore>() { new WebStore { WebStoreId = webStoreId, CustomerName = customerName } };
var webstore = new WebStore { WebStoreId = webStoreId, CustomerName = customerName };
//Set up Mock
_unitOfWork.Setup(uow => uow.GetRepository<WebStore>()
.Get(store => store.WebStoreId == webStoreId, null, "", 0, 0).FirstOrDefault()).Returns(webstore); **//Not working**
...
}
Based on @PeterCsala's help, it works as expected by using It.IsAny
:基于@PeterCsala 的帮助,它通过使用
It.IsAny
可以按预期工作:
_unitOfWork.Setup(x => x.GetRepository<WebStore>()
.Get(It.IsAny<Expression<Func<WebStore, bool>>>(), It.IsAny<Func<IQueryable<WebStore>,
IOrderedQueryable<WebStore>>>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(listWebstore);
There are two things that need to be fixed:有两件事需要修复:
Get
methodGet
方法If you rewrite your GetWebstorebyId
method like this:如果您像这样重写
GetWebstorebyId
方法:
var webstoreData = unitOfWork.GetRepository<WebStore>()
.Get(store => store.WebStoreId == webstoreId);
return webstoreData.FirstOrDefault();
then it become evident that you should mock Get
and not Get
+ FirstOrDefault
那么很明显你应该模拟
Get
而不是Get
+ FirstOrDefault
It.IsAny
during setupIt.IsAny
Because your mock does not rely on the incoming parameter that's why you don't need to specify it as a concrete value during the setup.因为您的模拟不依赖传入参数,这就是为什么您不需要在设置期间将其指定为具体值的原因。
_unitOfWork
.Setup(uow => uow.GetRepository<WebStore>().Get(It.IsAny<int>()))
.Returns(listWebstore);
With this you have set up your Get
method that it will receive an int
(the value does not matter) and returns a collection of WebStore
objects.有了这个,您已经设置了
Get
方法,它将接收一个int
(值无关紧要)并返回WebStore
对象的集合。
The FirstOrDefault
will be called on the returned mock value. FirstOrDefault
将在返回的模拟值上调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.