Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Creating Unit Tests for ASP.NET MVC Applications

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ASP.net] [www.ASP.net] [Stephen Walther] Hi, my name is Stephen Walther In this video I'm going to show you how you can unit test controller actions in an ASP.net MVC application. Now there are two very different audiences who might be interested in creating unit tests. First, you might be a tester, and in that case you would be interested in building unit tests to test an existing application's logic to make sure that everything works as expected. The nice thing is the ASP.net MVC framework was designed to be very testable. It was designed to support a very clean separation of concerns, so I think you'll be very happy with the sort of experience of trying to test an ASP.net MVC application. Now the other group that would be interested in unit tests are people interested in test driven development. This group—while this group is interested in unit tests, they're fundamentally focused on—they're primarily focused on using unit tests to drIve application design. They want to write their tests first and then write their application logic to satisfy the test, and they're using this as a design methodology. The ASP.net MVC framework was designed with this group of programmers in mind, and it's just a beautiful framework for— ASP.net MVC is just a beautiful framework for doing test-driven development so I think you'll be very happy with what I'm showing you today. I'm actually going to show you how to create a number of different types of tests. I'm going to show you a number of different ways of testing controller action. First I'm going to show you how to test whether a particular view is returned from a controller action, So you might want to call like the index controller action and make sure that the index view is actually returned when you call the index controller action. I'll show you how to test that. I'm also going to show you how to test ViewData. I want to show you how you can test whether the expected data was returned from a controller action. So you call a get product controller action, you want to make sure that you get a particular product back from it. That's very easy to test as well. Finally, I want to show you how to test other types of results— other results from a controller action. For example, you're testing controller action and you want controller action to automatically redirect you to another action. How do you test that? Well that's also very easy to do with the new ASP.net MVC controller architecture. So I'll be showing you how to do that. Let's go ahead and get started. What I'm going to do first is I'm going to go ahead and create a new project in Visual Studio 2008—a new ASP.net MVC application. Let me open up Visual Studio 2008, and I'm going to go to file, new project, and I'm going to select Visual Basic as my programming language and then select an ASP.net MVC web application to create over here. We're going to be creating a store, so I'm going to go ahead and name the new application "Store" and click okay. Now the first dialogue box that you get here is— notice it's asking you whether you would like to create a separate unit test project and, since that's what we're concerned about in this video, we definitely want to do that. Before I go ahead and click okay here I want to point out that Microsoft has given you a choice here. Notice down here where it says test framework it defaults to using the built-in testing capabilities included in Visual Studio Professional 2008 and above. That's what we're going to use in this video. We're going to use the unit testing framework built in to Visual Studio. But Microsoft has given you a choice. If you're a big fan of in-unit, for example, as a testing framework you can select that option as well. So you can select another testing framework if you have a preference in that area. But I'm going to go ahead and stick with Visual Studio— the Visual Studio unit test option—and click okay. If you look over in the solution explorer you'll notice that two projects are created. The first project up here is the actual ASP.net MVC application, and then there's a second project down here. That's the test project. The first thing I'm going to do is I'm going to add a new controller into my ASP.net MVC application. I want to create a new product controller, so I'm going to select add, new item, and I'm going to select a controller class, and I'm going to name it "ProductController.vb." There's the product controller. Notice we get a default index action. I'm going to go ahead and add another action onto here. I'm going to add a details action, and that will allow us to see product details. I'm going to go ahead and have it accept an Id parameter so that we can get details for a particular project for a particular product, and I want it to return a view, so I'm going to type in "Return View." One warning I have to give you here immediately: Normally you can just say return view and view will infer the name of the right view from the name of the controller action, so this will return a view called "Details." When you're unit testing, if you're interested in actually unit testing that the right view is being returned, you don't want to take advantage of this inference. You want to be explicit. That way it's easier to test later on. I'm going to go ahead and—whoops, that's the wrong name. I'm going to go ahead and be explicit about the particular view that I want to return here. So that's my controller. First thing I want to do is test to make sure that in fact the details view is returned from the details action. How do I do that? Well I'm going to go over to my unit test project over here, and I'm going to add a new unit test. I'm going to right click on this controllers folder, I'm going to go to add, and I want to go ahead and select this first option right here where it says new test. Now one big warning here. This is a very tempting option down here. You'll notice that there's another menu option that says "Unit Test..." That's a very tempting option because we are, in fact, trying to add a unit test, but you don't want to pick that option when you're building an ASP.net MVC application. This is a great option to pick when building a Webforms application because what it will do is it will automatically generate a unit test that launches a web server. We don't want to do that in an MVC application. We just want to pick new test—the new test option up here— and then in this dialogue box we want to pick unit test. Don't pick the unit test wizard. That's what we're trying to avoid. We want to just select unit test. Okay, so I'm going to create a unit test and name it "ProductControllerTest," and this will add a new unit test class into my unit test project. Here is the new test class. You could ignore most of the content inside this class. The really important part is any subroutine that starts with test method— any subroutine that starts with the test method attribute will be automatically run when you run your unit test. What we want to do is we want to create a test for that controller action that we just created, and we want to test that the details action returns the right view. I'm going to start by creating an instance of the controller. Before I can create an instance of the controller from my project I need to import the right name space. So up here at the top I'm going to say Imports Store—that's the name space I want— and I know in a second I'm going to need to take advantage of the system.web.MVC framework— the system.web,MVC namespace because that contains all of the different action results, and I'm going to need those down here in my test. Okay, going back down to the test, what I want to do here is I want to start by creating an instance of that product controller. We want to say Dim controller As New Product Controller, and then what I want to do is I want to get a result back from calling the details action on the product controller. We're going to say Dim result As ViewResult and that's going to be equal to controller.Details, and remember it took a parameter so we're going to pass in 2 as a parameter and there I have my result. One thing that I want to emphasize here is notice I don't have to do anything like mock the httpContext. I don't have to do anything weird about launching a web server or doing anything with surrounding context to get any of this to work because this is just like working directly with classes. I'm just instancing the classes, calling the methods, because all that context stuff is taken care of by the MVC framework. We can ignore it when we're doing the unit test. That makes unit testing this stuff extremely easy. So we've got our result back and I want to make sure that we got the right view, So I'm going to add an assert inside of here, and I want to check to make sure that we actually got the details view back. I can get that by saying result.ViewName and now I've finished my unit test. Let's go ahead and run it. I'm going to go ahead and click this icon up here to run all of the tests in the solution. You can also hit control RA to do the same thing, but I'm going to go ahead and click the icon. There it goes. It's running and you will notice that, hey, they all passed. This is the one I just created—TestDetailsView. It worked correctly. If I wasn't returning the right view— Let's change this to Detailss with an extra "s" and run the test again, you notice it will fail, so the unit test is working correctly here. Let me go ahead and fix this. The next thing I want to show you is how can you test that you're getting the right ViewData back from a controller action, so let me switch back to my product controller class, and I'm going to modify— I'm going to modify this method—this action, details, so it also returns a particular product with the details. What I'm going to do is I'm going to create a new instance of a product. I'm going Dim product As New Product, pass in the ID being passed over here, and this is the product name—I'm going to say Laptop— and there's a new product, and then I can go ahead and pass that as part of the view data by passing a second parameter into the view when I create it. So there, I've modified my controller action to return some ViewData, and now I want to test to make sure that I'm getting the right ViewData back. How do I do that? Let me switch back to our product controller test class over here, and I'm going to add in another test. Let me copy and paste this guy, our previous test, and I'm going to call this guy TestDetailsViewData, because that's what we're testing. Again, we want to create an instance of the controller. We want to get a result back. But in this case what we want to do is we want to make sure that we get the right ViewData back. What I'm going to do is I'm going to try to get a product out of my ViewData and that should be result.ViewData.Model, so that should be passed over as the model property on the ViewData, and then I'm going to test to see whether or not it says laptop— whether or not the name of the product coming back is in fact laptop. We're going to say product.Name, and there's my test. Let me go ahead and run this. Let me save it first. I'm going to run this by clicking this icon up here. It's running, and it looks like all of our tests passed. That's what this says right here. If we scroll down we can see TestDetailsViewData. So that was real easy. That was an easy way to test ViewData. The very last thing that I want to show you is how to test whether a different type of action result was returned from calling a controller action. So far we've looked at how we can test view results, but there are other types of things we might do in a controller action other than returning a view. For example, you might want to test that when you call the details action that you don't pass over a valid parameter. You might want to test that you're actually redirected off to the index action, so I'm going to show you how to test that. Let me copy and paste this existing test method, and I'm going to rename it to TestDetailsRedirect since we're going to be testing whether this redirects successfully. I can keep the first two lines of the test. I'm going to get rid of the last two lines. And I still want to create a controller. This time, instead of getting a view result back, what I want to do is I want to get a different type of result back. I want to get a redirect to route result back, and I'm going to get that by passing in an invalid Id for a product, so I'm going to pass in -1, and what I want to test— so I'm going to go ahead and put in my assert. What I want to test is I want to test to make sure that the person actually gets redirected off to the index action. I can do that by typing result.Values, and the pass in the action value, and what this will do is it will look up the value of—the action value, and check to make sure it's equal to index when I pass in an invalid Id. All right, so let me go ahead and run the test. It's going to fail the first time I run it because I haven't written this application logic yet. It has failed. Let's scroll down and see why it failed. It failed because it's telling us that we got a view result back and , hey, that's not a route result. That's the expected failure. So let's go back to our product controller class, and I'm going to add a little bit of logic here. I'm going to say If Id < 1 Then I want to go ahead and redirect them, so I'm going to return a RedirectToAction result and redirect them off to the index action. Otherwise, they can go ahead and do this stuff down here. So otherwise it's fine to go ahead and return the details view as normal and pass over the product. Okay, so let me do a build. Everything built successfully. Let me run my tests and, hey, this is beautiful. Everything passed. So we were able to successfully test whether or not we're getting a redirect back, which is what we wanted to test. In this video I've shown you sort of three very common tests that you might want to do on a controller action and, hopefully, I've conveyed to you the sense of just how easy it is to test with the ASP.net MVC framework. [Microsoft ASP.net] [www.ASP.net]

Video Details

Duration: 15 minutes and 50 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 5
Posted by: neudesicasp on Sep 16, 2013

Learn how to create unit tests for controller actions. In this tutorial, Stephen Walther demonstrates how to test whether a controller action returns a particular view, returns a particular set of data, or returns a different type of action result. For additional information about this video, read the "Creating Unit Tests for ASP.NET MVC Applications" tutorial (C#, VB).

Caption and Translate

    Sign In/Register for Dotsub to translate this video.