0

I am doing unit testing of one of my action method in ASP.NET MVC 4 application.

And the problem is there is one private property in the action method of that controller.Below is the code of Private property and method :-

Private Property :

    private int ProductId
    {
        get { return Convert.ToInt32(System.Web.HttpContext.Current.Session["FKProductID"]); }
        set { System.Web.HttpContext.Current.Session["FKProductID"] = value; }
    }

Method :

    public ActionResult GetDetails(string Name, int area, int ProdCategoryId = 0)
    {

        ProductCategoryViewModel model = new ProductCategoryViewModel
        {
            Area= area,
            FKProductID = ProductId, //private property

        };


       else if (model.FKProductID > 0)
       {
           ProductDto product= ProductService.GetProductDetails(model.FKProductID);

       }

Unit test method code :

    public void SelectProductTest()
    {
            // Act           
            var result = controller.GetDetails(name,area, prodcatid) as PartialViewResult;

    }

As i passed the three required parameters in the test method but i am getting problem on private property place(when the control rich's it).

Can anyone let me know how to deal with such a condition ?

Pawan
  • 2,150
  • 11
  • 45
  • 73
  • ..Are you Mocking the session ? – Sai Avinash Jan 09 '14 at 12:22
  • No there is no Mocking. – Pawan Jan 09 '14 at 12:24
  • In principle you shouldn't be testing private properties, because your client code cannot see them directly either. I don't see what the problem is in your case though - the code that you posted should work fine (although I question the direct use of the `System.Web.HttpContext` in a model class). – CompuChip Jan 09 '14 at 12:27
  • @Pawan..when you are not mocking the Sessionn , you would obviously get an error even it might be a public property or private property,some way you need to mock the session you are using there. mocking is required, beacause while running your unit test, you are not actually running the application , so there will not be any session data , it has to run in the assumption that session is available. this is where mocking comes into picture.. – Sai Avinash Jan 09 '14 at 12:30
  • @CompuChip.....The problem arrives at the time of unit testing when control reach the model(ProductCategoryViewModel) properties section and when it goes to the private property statement(FKProductID = ProductId) then gets its value 0.And there is a condition ahead that checks the private ProductId in if.You can see that in updated post. – Pawan Jan 09 '14 at 12:40

2 Answers2

0

You can use reflection to get this data:

How do I test a private function or a class that has private methods, fields or inner classes?

Different language/technology but the approach is the same.

On .Net: Get private Properties/Method of base-class with reflection

Real context:

We take over a project designed and implemented by a group of students. There is no documentation. The authors have long ago escaped. The customer wants to extend the functionality with backward compatibility. You are forced to do tests that are not available to be able to maintain backward compatibility. Well, here sooner or later there is a need to test private variables. You test them to understand the project and see that you haven't broken it.

To say that test should not be supposed to check private properties is academic nonsense to me.

Writing missing tests and using traps and implement a requirements etc. by using 'CodeContract' approach will let you master the project otherwise you are sailing in the wrong direction.

https://www.macs.hw.ac.uk/~hwloidl/Courses/F21SC/CodeContracts_UserManual.pdf

Sorry for My English.

user1785960
  • 565
  • 5
  • 17
-1

Private code can't be accessed from outside by design - that's the entire point of the private keyword. So there's no way of testing it.

Instead, the usual way is to declare your property as internal and then grant your test assembly access via the InternalVisibleTo attribute. This way, you won't break encapsulation, because internal members behave exactly like private members in every other respect.

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34