I have an API as below:
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService;
}
//GET: api/values
[HttpGet]
public MyOutput<MyEntity> Get(string f, string o)
{
var fItems = JsonConvert.DeserializeObject<Dictionary<string, string>>(f);
var oItems = GetDictionaryFromStr(o ?? "");
var myInput = new MyInput<MyEntity>()
{
PredicateDictionary = fItems,
OrderByDictionary = oItems
};
var result = _myService.Search(myInput);
return result;
}
It works well. Now, I want to write a unit test for my API, using Moq and Xunit;. I want to set the expected values of result, then mock my DIs and call the controller, What I expect is that the return value of controller and my results be equal. but I don't know why result in var result = api.Get(f, o); is null after returns from controller. Is there anything wrong whit my test?
[Fact]
public void Should_ReturnResult_When_CallingMyApi()
{
//Arrange
var f = "{'Currency':'UR'}";
var o = "+Amount";
var fItems = JsonConvert.DeserializeObject<Dictionary<string, string>>(f);
var oItems = GetDictionaryFromStr(o ?? "");
var baseServiceMock = new Mock<IMyService>();
baseServiceMock
.Setup(x => x.Serach(It.Is<MyInput<MyEntity>>
(i => i.PredicateDictionary== fItems
&& i.OrderByDictionary == oItems
&& i.Paging == pagingItems
)))
.Returns(new MyOutput<MyEntity>()
{
OrderByDictionary = oItems,
PredicateDictionary = fItems
});
var api = new MyController(baseServiceMock.Object);
//Act
var result = api.Get(f, o);
////Assert
Assert.Equal(result.PredicateDictionary, fItems);
Assert.Equal(result.OrderByDictionary, oItems);
}
Update:
Also I changed the baseServiceMock, with and without the It.Is. In case of It.Is I added
baseServiceMock
.Setup(x => x.Search(It.Is<MuInput<MyEntity>>
(i => i.PredicateDictionary.Keys == fItems.Keys
&& i.PredicateDictionary.Values == fItems.Values
&& i.OrderByDictionary.Keys == oItems.Keys
&& i.OrderByDictionary.Values == oItems.Values
&& i.Paging == pagingItems
)))
.Returns.....
It.IS<>and collections. while troubleshooting you could useIt.IsAny<MuInput<MyEntity>>. Also you can try usingCollectionAssertIsAnywhile debugging the problem. That is just so you can check that a result is actually being returned. Once that is confirmed then you can change it back to look at the other possibility that your asserts are not comparing the collections properly. With your current filter are you getting past theActto yourAsserts?