mercredi 4 mai 2016

How to retrieve value of paramters from returns of mock in Moq in ASP.NET MVC unit test

I am developing a ASP.NET MVC project. I am doing Unit Test to each component. I am using Moq to mock my repositories. But I am having a problem in mocking a function. Please see my scenario below.

This is my test method

[TestMethod]
public void Cannot_Edit_If_Invalid_Region()
{
      Region[] regions = { 
                             new Region{
                                  Id = 1,
                                  Name = "Test 1"
                             },
                              new Region{
                                   Id = 3,
                                   Name = "Test 3"
                              },
                              new Region{
                                  Id = 4,
                                  Name = "Test 4"
                              }
                          };
      Mock<IRegionRepo> mock = new Mock<IRegionRepo>();
      mock.Setup(m=>m.Region(It.IsAny<int>())).Returns(regions[It.IsAny<int>()]); // problem is here
}

As you can see in the above code I commented where the problem is. Actually how I want to mock is I pass a parameter to function, then returns will retrieve one of the regions by paramter passed to function using as index of array.

This is simple of what I want

 mock.Setup(m=>m.Region("parameter passed").Returns(regions["parameter passed"]);

So how can I get it? How can I retrieve the parameter passed to mock function from returns please? Please help me. How can I get it?

Aucun commentaire:

Enregistrer un commentaire