Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, February 28, 2010

NUnit templates for ASP.NET MVC 2 RC 2

Now with ASP.Net MVC 2 RC 2 out, it's time to have a working NUnit template.

I tried to use the NUnit MVC 2 templates others have made available, however, I had trouble installing them with the batch file. Once I installed it manually, the results didn't compile. I discovered it was because these were altered from the NUnit MVC 1 templates. I decided to start over from the MSTest template, modifying it for NUnit. Starting from the supplied templates will make it easier to stay in sync with the ASP.NET MVC team.

The instructions here were a start, however, I took it a step further and synchronized most of the template code with the MVC 2 template code. This gave me a really good result and is really quite workable.

Try it for yourself. Get started with the following instructions:

Download the zip file: Mvc.NUnit.Testsv2.0.zip

Copy the included "MvcApplication.NUnit.Testsv2.0.cs.zip" to "%ProgramFiles(x86)%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test".
If on a x64 bit machine, then %ProgramFiles(x86)% normally equals "C:\Program Files (x86)", otherwise, it's the normal location of "C:\Program Files".

Import the correct registry file (NUnit.reg or NUnit64bit.reg) to register the new template with MVC 2.

Open the "Visual Studio 2008 Command Prompt", running as administrator. Then run "devenv /setup". Once that is done, the template should be ready to use.

These templates require NUnit 2.5 or higher. Since I don't use VB.Net, I didn't make a corresponding template, if someone would like to contribute that, please feel free.

Monday, September 7, 2009

Using NHibernate ActiveRecord and LINQ

I'm trying to learn NHibernate ActiveRecord and LINQ all in the same day. I ran into the following exception:
Session is closed!
Object name: 'ISession'.

When trying to execute the following code:
      public static User FindByUserName(string username)
{
// Note that we use the property name, _not_ the column name
return (from p in User.Queryable
where p.Username == username
select p).First();
}

It took me a couple hours to get by this one, so I thought I would post it. I had to wrap it in a using (new SessionScope()) statement. Like this:
       public static User FindByUserName(string username)
{
using (new SessionScope())
{
// Note that we use the property name, _not_ the column name
return (from p in User.Queryable
where p.Username == username
select p).First();
}
}


Happy Coding.