How to resolve file path with NUnit

Stesvis 1,041 Reputation points
2023-01-23T01:02:48.9266667+00:00

I have an ApiController where I work with a file stored in some solution folder:

using var reader = new DatabaseReader(Path.Combine(_environment.ContentRootPath, "Data/GeoLite2-City.mmdb"));

Then I have a unit test where I test that method.
I use Mock to mock the IWebHostEnvironment, but when I run the test I always get that _environment.ContentRootPath is always null.

How can I solve this? Thanks!

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. Stesvis 1,041 Reputation points
    2023-01-23T16:38:09.3633333+00:00

    Hi @Anonymous thanks for the reply, I am gonna share some more code.

    LookupController

    [ApiController]
    [Route("api/[controller]")]
    public class LookupController : ControllerBase
    {
        private readonly ILogger<LookupController> _logger;
        private readonly IWebHostEnvironment _hostEnvironment;
    
        public LookupController(ILogger<LookupController> logger, IWebHostEnvironment hostEnvironment)
        {
            _logger = logger;
            _hostEnvironment = hostEnvironment;
        }
    
        [HttpPost("city-info")]
        public ActionResult<IEnumerable<CityInfoResource>> LookupCityInfo([FromBody] IEnumerable<string> ips)
        {
                //...
    
                using var reader = new DatabaseReader(Path.Combine(_hostEnvironment.ContentRootPath, "Data/my-file.mmdb"));
    
                //...
        }
    }
    
    

    LookupControllerTests

        [TestFixture]
        public class LookupControllerTests
        {
            private readonly Mock<ILogger<LookupController>> _logger;
            private readonly Mock<IWebHostEnvironment> _hostEnvironment;
    
            public LookupControllerTests()
            {
                _logger = new Mock<ILogger<LookupController>>();
                _hostEnvironment = new Mock<IWebHostEnvironment>();
            }
    
            [Test]
            public void Test_CityLookup_With_Valid_IPs()
            {
    			//...
                var controller = new LookupController(_logger.Object, _hostEnvironment.Object);
                var response = controller.LookupCityInfo(ips);
    			//...
            }
        }
    
    

    The problem is with Path.Combine(_hostEnvironment.ContentRootPath, "Data/my-file.mmdb")

    When I run the app, the ContentRootPath is "R:\\Demos\\1\\ip-lookup-app" and the file is found.

    When I run the tests, the ContentRootPath is always null.

    I already found that thread yesterday, but I could not solve it, I always got it null.

    So for now I just copied the same file and folder under the Tests project and changed the controller to using var reader = new DatabaseReader("./Data/GeoLite2-City.mmdb"); so basically the path is relative to either project, but of course it's a bad solution.

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-01-23T16:56:24.89+00:00

    you never defined the value to use for ContentRootPath in the Mocked IWebHostEnvironment. if you do not define the Mocked values, they will be null. See docs for your mocking library to initial values


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.