moq objects Returns method, should return a null object

C#asp.net Mvc-3MockingMoqWcf Web-Api

C# Problem Overview


I'm developing a Web API, and one of the test I came up with is that, if client makes a GET operation with a Physical Test ID (Physical Test is the resource I'm looking for) and that physical test is not found, the web API should return a 404 status.

Now, I'm using moq framework for my tests and I have the following code:

[TestMethod]
public void then_if_physical_test_not_found_return_not_found_status()
{
    var unitOfWork = new Mock<IUnitOfWork>();
    var repository = new Mock<IRepository<PhysicalTest>>();
    repository.Setup(r => r.FindById(It.IsAny<int>())).Returns();
    unitOfWork.Setup(m => m.PhysicalTests).Returns(repository.Object);
    var pt = new PhysicalTestResource(unitOfWork.Object);
    HttpResponseMessage<PhysicalTest> response = pt.GetPhysicalTest(43);
    Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode)
}

I need the Returns() method to return a null object, which is going to be what the actual API method would return if the resource is not found.

I tried sending null as a parameter in the Returns() method but had no success.

C# Solutions


Solution 1 - C#

You don't indicate what the error was, but this should work:

unitOfWork.Setup(m => m.PhysicalTests).Returns((IRepository<PhysicalTest>)null);

I suspect you tried to call it with Returns(null), which causes the compiler to complain since Returns is overloaded and it doesn't know which method should be called. Casting to a specific type removes the ambiguity.

Solution 2 - C#

rt is a return type of method: FindById

repository.Setup(r => r.FindById(It.IsAny<int>())).Returns(Task.FromResult((rt)null));

Solution 3 - C#

If you are receiving an error like this:

enter image description here

You just need to specify the input parameter of 'Returns' method. Take a look in my example:

_ = _fileStorage.Setup(x => x.LoadDocument(It.IsAny<string>())).Returns(value: null);

Solution 4 - C#

Organization is a return type of method: Get

mockCache
    .Setup(cache => cache.Get(It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()))
    .Returns(value: null as Organization);

Solution 5 - C#

You could try this:

ref1.Setup(s => s.Method(It.IsAny<Ref2>(), It.IsAny<string>()))
     .Returns((Task<Ref3>)null);

ref1 = Mock Interface
Ref2 = Type request parameter
Ref3 = Type of return method mock

Solution 6 - C#

You're too close, you only need to pass the return type as generic type like so

repository.Setup(r => r.FindById(It.IsAny<int>())).Returns<IRepository<PhysicalTest>>(null);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanielView Question on Stackoverflow
Solution 1 - C#Jeff OgataView Answer on Stackoverflow
Solution 2 - C#Radhika PatwariView Answer on Stackoverflow
Solution 3 - C#Jorge FreitasView Answer on Stackoverflow
Solution 4 - C#Thanh TruongView Answer on Stackoverflow
Solution 5 - C#Anderson ClementeView Answer on Stackoverflow
Solution 6 - C#Ali JamalView Answer on Stackoverflow