0

I am totally brand new to Unit Testing and am trying to learn off my own back. I have failed at the first hurdle and despite countless books and web sites I cannot see what I am doing wrong... Please can someone tell me?? I have an Action ViewResult as follows I am looking to test:

    public ViewResult ProductList()
    {
        List<ProductItem> allProducts = CallGetAllProducts();
        return View(allProducts);
    }

You can see it is calling a function:

    private List<ProductItem> CallGetAllProducts()
    {
        var productFilePath = Server.MapPath(@"~/CSV/products.csv");
        var products = new ProductsCSV();

        return (products.GetAllProductsFromCSV(productFilePath));
    }

Which is in turn calling the following function:

    public List<ProductItem> GetAllProductsFromCSV(string csvPath)
    {
        String[] csvData = File.ReadAllLines(csvPath);
        List<ProductItem> result = new List<ProductItem>();

        foreach (string csvrow in csvData)
        {
            var fields = csvrow.Split(',');
            ProductItem prod = new ProductItem()
            {
                ID = Convert.ToInt32(fields[0]),
                Description = fields[1],
                Item = fields[2][0],
                Price = Convert.ToDecimal(fields[3]),
                ImagePath = fields[4],
                Barcode = fields[5]
            };
            result.Add(prod);
        }
        return result;
    }

I have a Unit Test project I have created with the following test method in the appropriate ControllerTest file:

    [TestMethod()]
    public void ProductListGetsViewResult()
    {
        ProductsController controller = new ProductsController();
        ViewResult result = controller.ProductList() as ViewResult;
        Assert.IsNotNull(result);
    }

I have all the correct references setup as far as I can see, however the results of running this test are coming back with:

Result Message:
Test method ShoppingBasket.Controllers.Tests.ProductsControllerTests.ProductListGetsViewResult threw exception: System.NullReferenceException: Object reference not set to an instance of an object.

It looks like the problem is with the calling of the ViewResult in the 'Act' part of the process... But Why, I dont know.

CJH
  • 929
  • 11
  • 32
  • 1
    If I were faced with this, the first thing I would do is run the test in the debugger, and discover exactly which line of code is throwing the exception. Much better than guessing. – hatchet - done with SOverflow Aug 03 '17 at 22:44
  • Also, my guess is that the exception is thrown in the `CallGetAllProducts` method, but without knowing what that code looks like, can't say more than that. – hatchet - done with SOverflow Aug 03 '17 at 22:58
  • Thanks for the feedback hatchet... I have seen the debugger mode but havent got that far yet (total novice here). Could it just be that that the CallGetAllProducts is able to return a null causing it... I will post that function in the Q above in a moment... Thank – CJH Aug 03 '17 at 23:00
  • Get out the debugger and see which object is null. It will probably be something to do with the context the Unit Tests are run from, eg it uses a different .config file. – Jeremy Thompson Aug 03 '17 at 23:23
  • Thanks Jeremy.. I heeded your advice and debugger pointed me at the Server.MapPath call on the second code block down from above. I am intrigued where you say 'running from a different .config file... Could you spill any more knowledge on that to me please? – CJH Aug 03 '17 at 23:44
  • @CJH `Server.MapPath` is the cause of the problem. it is provided by IIS during run time but does not exist when testing as IIS is not available. Check the answer I gave here https://stackoverflow.com/questions/38452743/how-to-avoid-httpcontext-server-mappath-for-unit-testing-purposes/38456288#38456288 – Nkosi Aug 03 '17 at 23:53
  • @CJH you would also need to abstract the `File.ReadAllLines` IO access to be able to mock it and test in isolation. – Nkosi Aug 04 '17 at 00:04

0 Answers0