locked
MVC Unit Testing RRS feed

  • Question

  • User1997074450 posted

    How to start doing unit testing on your code?

    Friday, May 28, 2021 7:13 PM

Answers

All replies

  • User-474980206 posted

    this is a big topic, and requires you to write your code so that it is testable.  you should do some studying on unit testing before you start (just google).  than see docs:

       https://docs.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2019

      https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2021 7:28 PM
  • User475983607 posted

    How to start doing unit testing on your code?

    I recommend learning unit testing concepts next go through a tutorial based in the unit test framework you are using.   

    https://docs.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2019 

    https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019&tabs=mstest

    Friday, May 28, 2021 7:31 PM
  • User1997074450 posted

    This is my first test case to check if the view exists: It fails not sure why?

    public void Index()

    {

    HomeController controller = new HomeController();

    ViewResult result = controller.Index() as ViewResult();

    Assert.IsNotNull(result);

    }

    Friday, May 28, 2021 7:41 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2021 8:29 PM
  • User1997074450 posted

    I have two controllers. One pass the test using the code in the documentation you provided

    other one fails saying object reference not set to an instance of an object.

    It is pointing to the SQL Connection I made:

    private SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString();

    Code works fine. How to get past this ....

    Friday, May 28, 2021 9:32 PM
  • User-474980206 posted

    your unit tests should not call sql server. you need to replace the sql sever call with a mocked component. this will probably require using injection. for classic MVC where construction injection is not available you supply an overload to set 

    to mock you will need to create an interface for you sql update layer (if you haven't already). then you create a mock object based on the intake either in the test project or via a mocking library.

    say you interface is IMyDbContext, then the controller typically will look like

    public class MyController
    {
         private IMyDbContext dbContext;
    
        // framework constructor
        public MyController() 
        {
           dbContext = new MyDbContext();
        }
    
        // unit test constructor
        public MyController(IMyDbContext dbContext) 
        {
           this.dbContext = dbContext;
        }
    
       // some action to test
       public IAuctionResult MyAction(MyModel model)
       {
          ...
          dbContext.Save(myEntity);
          ...
          return View(...);
       }
    }
    

    the unit test

    var db = new MockDbContext();
    var controller = new MyController(db);
    var model = new MyModel {....};
    
    // call action
    controller.MyAction(model);
    
    // assert db updates were called with correct parameters
    Assert.AreEqual(db.SaveMethodCalled.Length,1,"should have called save once");

    be sure to study mocking, and testing that mocked methods were called, and properties. there are also sdk's to create mocked objects.

    Friday, May 28, 2021 10:13 PM
  • User1997074450 posted

    Thank you! 

    The way its working in my app:

    View (display)

    Controller (Business logic)

    Repository (Database calls)

    Saturday, May 29, 2021 2:46 AM