Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 506 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 506 days ago
  • azamsharp iOS 7 BETA installed successfully! about 506 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 506 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 506 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 506 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 506 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 507 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 507 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 507 days ago

AzamSharp Some day I will know everything. I hope that day never comes.

The ASP.NET Model View Controller application allows you divide your application into layers. This division can result in better testing and clean reusable components. Let's see a very simple application using MVC project which involves unit testing. The application consists of a controller named "ProductController".  

public class ProductController : Controller
    {
        private NorthwindDataContext northwind;

        public ProductController()
        {
          
        }

        [ControllerAction]
        public void Index()
        {
            RenderView("Index"); 
        }

     
        [ControllerAction]
        public void List()
        {
            string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
            northwind = new NorthwindDataContext(connectionString);
            List<Product> products = northwind.Products.ToList<Product>();   
            ViewData.Add("Products", products);
            RenderView("List");
        }        
    }

The List() action gets the products from the database and add it in the ViewData dictionary.

Now, add a new class library project for testing purposes. Make references to the testing framework which in my case is "MbUnit". Now add a reference to the MVC application from the test application. When you compile you might get some errors because your test application is not referring to the "system.web.extensions". The interesting thing is that even if you add a reference using the "Add Reference" feature the error won't go away. This is because the ASP.NET compiler is looking for version 3.6 of "system.web.extensions" library and when you add a reference you can only see version 2 and version 3.5. I know this is strange! So, I went to GAC and manually copied the version 3.6 of the dll into some other place "C:\". Now, I added a reference to that dll located at "C:\" from my test project and now everything is good.

In order to test your ProductController you need to sub class it. This is because of the RenderView method which is called in the ProductController "List" action. When the RenderView is fired in the ProductController it won't find the "List" view this is because you are executing the controller from the test project. So, let's create a fake controller.

 public class FakeProductController : ProductController
    {
        public string RenderViewAction { get; private set; }

        protected override void RenderView(string viewName, string masterName, object viewData)
        {
            RenderViewAction = viewName;
            Console.WriteLine("Fake Controller RenderView is fired");
        }       
        
    }

In the above code I have override the RenderView method.

Now, I can implement the test.

 [TestFixture]
    public class TestProductController
    {
        [Test]
        public void test_can_load_all_products()
        {
            FakeProductController controller = new FakeProductController();

            controller.List();

            Assert.IsNotNull(controller.ViewData["Products"]);

            Assert.IsTrue( ((List<Product>)controller.ViewData["Products"]).Count > 0);
        }

        [Test]
        public void test_can_invoke_index_action()
        {
            FakeProductController controller = new FakeProductController();

            controller.Index();

            Assert.AreEqual("Index", controller.RenderViewAction);
        }
    }

Posted on Wednesday, January 23, 2008 1:25 PM | Back to top


Comments on this post: Unit Testing ASP.NET Model View Controller Application

# re: Unit Testing ASP.NET Model View Controller Application
Requesting Gravatar...
How would test javascript execution running in the view ?

Frederic Torres
www.InCisif.net
Web Testing with C# or VB.NET
Left by ftorres on Jan 24, 2008 3:56 PM

# re: Unit Testing ASP.NET Model View Controller Application
Requesting Gravatar...
Hi,

I have not tested out JavaScript using MVC but you can always use JsUnit for testing.
Left by Mohammad Azam on Jan 25, 2008 8:36 AM

Your comment:
 (will show your gravatar)
 


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free