views:

613

answers:

4

I have code in the top layer of my .Net web application that I'd like to unit test, but when my build server compiles the project using the aspnet_compiler.exe, it makes a .dll file that is not at all usable by another project, i.e. an NUnit test project.

(This is true of ASP .Net web applications and of ASP .Net MVC applications.)

Am I doing something wrong here? Here's my NAnt script that calls the compiler...

<exec program="${asp.compiler.home}/aspnet_compiler.exe" failonerror="true">
   <arg value="-nologo"/>
   <arg value="-c"/>
   <arg value="-f"/>
   <arg value="-errorstack"/>
   <arg value="-v"/>
   <arg value="${project.name}"/>
   <arg value="-p"/>
   <arg value="${project::get-base-directory()}"/>
   <arg value="${web.deploy.dir}\${project.name}"/>
  </exec>
A: 

Can't you run something like here, instead of in Nant as a post-build event?

C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v / -p "$(SolutionDir)\PathToMyWebProject"

(where FilePathToMyWebProject is the path to your project file relative to the solution file)

Dan Atkinson
A: 

We use MSBuild with a build file to compile the web app and run tests, if you can skip the NAnt stuff, here is a relevent section from the build file (called as a parameter to MSbuild.exe):

<!-- Build projects by calling the Project files generated by VS -->
  <Target Name="Build">
    <MSBuild Projects="$(ProjectFile)" />
    <MSBuild Projects="$(TestProjectFile)" />
  </Target>

  <!-- Run Unit tests -->
  <Target Name="Test" DependsOnTargets="Build">
    <CreateItem Include="ClearViewTest\Bin\Debug\ClearViewTest.exe">
      <Output TaskParameter="Include" ItemName="ClearViewTest" />
    </CreateItem>
    <NUnit Assemblies="@(ClearViewTest)" ToolPath="C:\Program Files\NUnit 2.4\bin" ContinueOnError="false" OutputXmlFile="SoultionTestResults.xml" />
  </Target>
Decker97
Well, funny you should say that - I followed some advice that I should use MSBuild to build my DLL components and use the aspnet_compiler for web-based projects... should I revert to using MSBuild for everything? If so, what's the point in aspnet_compiler??
Brett Rigby
@Brett: Compiling views which otherwise wouldn't get compiled during a normal build process. Doing the MSBuild AFAIK doesn't compile them.
Dan Atkinson
@Dan: So, if I am trying to compile the code element, for the sake of unit testing, I can just use MSBuild to compile the web project and disregard the aspnet_compiler.exe?
Brett Rigby
My understanding (though limited) is that the msbuild will call the asp_complier - is this correct?
Decker97
+1  A: 

I have code in the top layer of my .Net web application that I'd like to unit test [...]

Stop right there; that's the problem. Put that code into a helper, and test it outside of ASP.NET.

Craig Stuntz
+1  A: 

You don't need to use aspnet_compiler.exe. That is just a utility application for precompiling your aspx pages to avoid the startup lag when a user hits a page for the first time.

As I understand it, any non-aspx/ascx code in your ASP.NET MVC web application will be compiled normally into a DLL when your solution is built. This DLL is then usable by your NUnit test project. I assume it's those bits you want to test.

So, just build the project using MSBuild from NAnt and forget about aspnet_compiler.exe.

Andy Grout
Ah, ok - I'll try it out in the week, but that could be good if I don't need to use the aspnet_compiler.exe. But you're saying the only downside to NOT using aspnet_compiler, would be that the .aspx pages are not precompiled, so the first visitor to the site would experience a delay before the site appeared, and from then on, it would be ok?
Brett Rigby
Absolutely correct. The delay would be for the first visit to each page. Once every page is visited then no more delay would be experienced. We're not talking about a massive delay, anyway, which is why aspnet_compiler.exe is not used more often.As I see it, aspnet_compiler.exe is more of a deploy-time consideration than a core build-time one (though if you're building your deployment package in your build then it should be part of NAnt buildfile). This is the clearest explanation of it I've read:http://blogs.msdn.com/apinedo/archive/2007/03/15/precompiled-asp-net.aspx
Andy Grout