Home > Software design >  Get single object from MOQ using Unit Testing, Unit of Work and Generic Repository Pattern framework
Get single object from MOQ using Unit Testing, Unit of Work and Generic Repository Pattern framework

Time:11-01

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,

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>();
    ...
}    

Methods

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 methods

[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** 

    ...
}

CodePudding user response:

There are two things that need to be fixed:

Mock the Get method

If you rewrite your GetWebstorebyId method like this:

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

Use It.IsAny during setup

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.

The FirstOrDefault will be called on the returned mock value.

CodePudding user response:

Based on @PeterCsala's help, it works as expected by using 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);
  • Related