ASP.NET MVC 2
The Model-View-Controller (MVC) pattern is an architectural design principle that separates the components of a Web application. This separation gives you more control over the individual parts of the application, which lets you more easily develop, modify, and test them.
ASP.NET MVC is part of the ASP.NET framework. Developing an ASP.NET MVC application is an alternative to developing ASP.NET Web Forms pages; it does not replace the Web Forms model.
If you have installed Visual Studio 2010, ASP.NET MVC 2 is already installed on your computer. You can get the most up-to-date version of the ASP.NET MVC 2 framework from the ASP.NET MVC download page.
Unit Test
If you create new ASP MVC 2 Web Application project, this is the view you get in VS 2010:You just need to pick ASP .NET MVC 2 under a list of available templates, provide a name for your project (or stick with default, but this is definitely not recommended for any production code – you wouldn’t like to have your projects named ASP MVC app2, app3, etc….) and click OK. This is exactly the same as for any other kind of project templates available in Visual Studio. But for ASP MVC 2 there is a additional step involved:
You can choose for Visual Studio to create a unit test project automatically for you. After it’s created it will contain unit tests for the controllers used in MVC project (17 unit tests all together). Out of the box Visual Studio supports adding unit test project for Visual Studio Unit Test framework only. However for some reasons you maybe prefer to use another test framework for excample NUnit, MbUint, or XUnit.
In versions of Visual Studio that support unit-test projects, you can create a custom test project template that will then be available as a project option in the MVC project dialog box. The custom test project can use a unit-test framework that you specify. In addition, you can include other libraries in the custom test project, such as a mock-object framework, a personal library of unit-test code, and so on.
Adding a new MVC test framework is a two-stage process. First, you register the third-party test framework with the MVC test dialog box, so that it can be selected when you create a unit-test project. You then create a test-project template for Visual Studio. When you are finished, you will be able to create a test project that is based on the newly added framework whenever you create a new MVC application.
Creating a Template for an MVC Test Project
1.Create a new class library project.
We recommend that you give the project a name that describes the test project, such as "MVCAppNUnitTests".
2.In the new project, add references for the following DLLs:
- System.Web.Abstractions
- System.Web.Mvc
- System.Web.Routing
These DLLs are installed in the global assembly cache (GAC).
3.Add references for the DLLs of the test framework and mock-object libraries.
4.Make sure that the Copy Local property for all references that you have added is set to true.
5.Add the folders and classes that your template requires.
At a minimum, we recommend that you add the following folders and classes for testing the default MVC application:
- Add a Controller folder and a Routes folder under the project root.
- In the Controller folder, add a class named HomeControllerTests.
- Add unit-test methods to the HomeControllerTests class for testing the Index and About action methods.
- In the Routes folder, add a class named RouteTests.
- Add unit-test methods to the RouteTests class for testing the default routes.
6.Build the project so that the required binary files are copied to the Bin folder.
7.In the File menu, click Export Template.
8.Select Project template, and then click Next.
9.Clear the Automatically import the template into Visual Studio check box and then click Finish.
The project template is exported as a compressed (.zip) file. A Windows Explorer window opens and displays the contents of the folder where the exported template is.
10.Move the exported template to the following folder:
%Program Files%|Micrososft Visual Studio 10.0|Common7|IDE|ProjectTemplates|C#|Test
Registering NUnit Test Framework
1.In Windows, open Registry Editor.
Caution
We recommend that you back up the registry by exporting it to a file before you make any changes to it.
2.Locate the following key:
HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftVisualStudio10.0MVC2TestProjectTemplates
3.Under TestProjectTemplates, add a key for the test framework, such as "NUnit".
4.Under the test framework, add a key for the code language, such as "C#" or "VB".
5.Add the following string values to the test framework key:
AdditionalInfo (Optional). Enter a URL that links to a Web page about the test framework. When a user clicks the Additional Info link in the Create Unit Test Project dialog box, the Web page that you specify is displayed.
Package (Optional). Enter the path of a Visual Studio 2008 package. You might need this if your test template adds UI elements, provides localized resources, or provides other advanced functionality.
Path. Enter the path where the template is located. The Visual Studio test framework requires that your test templates be registered with Visual Studio and located in the standard location for Visual Studio project templates, which is the following folder:
%Program Files%|Microsoft Visual Studio 10.0|Common7|IDE|ProjectTemplates
Template. Enter the file name of your test template, such as MVCApplicationNUnitTests.zip.
TestFrameworkName. Enter the name of the test framework the way that you want it to appear in the Test framework list of the Create Unit Test Project dialog box.
6.Close Registry Editor.
Install NUnit Test Project Template within Visual Studio 2010
1. Open the Windows Command window and change to the following folder:
%Program Files%Microsoft Visual Studio 9.0Common7IDE
Note: on Windows 7 you must run Command window as Administrator,otherwise the operation will failed.
2. Enter the following command:
Open Visual Studio.
In the File menu, click New Project.
Under Project types, expand the code language node.
Click Test.
If the template was added correctly, you see it listed under Visual Studio installed templates.
Testing the Project Template
"Template"="MVCAppNUnitTests.zip"
"Path"="CSharpTest"
"TestFrameworkName"="NUnitFramework"
"AdditionalInfo"=" http://www.nunit.org/index.php?p=home"
if Exist "%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDEVWDExpress" (
Set DevEnvDir=%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDEVWDExpress
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit64bitExpress.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit64bitExpress.reg"
goto express
) else if Exist "%ProgramFiles%Microsoft Visual Studio 9.0Common7IDEVWDExpress" (
Set DevEnvDir=%ProgramFiles%Microsoft Visual Studio 9.0Common7IDEVWDExpress
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnitExpress.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnitExpress.reg"
goto express
) else if Exist "%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDE" (
Set DevEnvDir=%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDE
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit64bit.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit64bit.reg"
goto vsts
) else if Exist "%ProgramFiles%Microsoft Visual Studio 9.0Common7IDE" (
Set DevEnvDir=%ProgramFiles%Microsoft Visual Studio 9.0Common7IDE
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit.reg"
goto vsts
)
else (
echo Error: Cannot locate Orcas install location.
goto end
)
)
echo Error: Cannot find Orcas install at: %DevEnvDir%
goto end
)
mkdir "%DevEnvDir%ProjectTemplatesVisualBasicTest"
)
mkdir "%DevEnvDir%ProjectTemplatesCSharpTest"
)
copy "%CopyFrom%CSharpMvcApplication.NUnit.Tests.zip" "%DevEnvDir%ProjectTemplatesCSharpTest"
mkdir "%DevEnvDir%ProjectTemplatesVisualBasicTest"
)
if NOT Exist "%DevEnvDir%ProjectTemplatesCSharpTest" (
mkdir "%DevEnvDir%ProjectTemplatesCSharpTest"
)
echo Running devenv.exe
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1039070/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/13651903/viewspace-1039070/