Yes, it’s gonna be largely a matter of personal style and preference, just like many prefer invoking
The first part is easy, just grab the Unity DLLs and reference them in your MVC project. At this point, you’re now free to use Unity to inject whatever you feel like. Rigging up the controller factory so that it will use Unity to resolve controllers is equally easy because the mvc contrib codeplex site has already created a class called UnityControllerFactory that does just this. Once you’ve added references to Unity and Mvc.Contrib.Unity from your MVC project, you can change your global.asax.cs file to look something like this:if (_container == null)
_container = new UnityContainer();
_container.RegisterInstance(typeof(AccountController), new AccountController());
I discovered that if you add the following to the relevant NUnit config file you can run a test dll built for .NET 4.0 in Visual Studio 2010 beta
Under <configuration> add:
<requiredRuntime version="v4.0.20506" />
and under <runtime> add:
<loadFromRemoteSources enabled="true" />
Update: a new version support MVC 2.0
To install the templates, just extract the zip file from the link below and run installNUnit.cmd. If you are installing on Vista or higher, run the cmd file as an admin.
Download -> NUnit Test Templates
Reasons for use
In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an object has any of the following characteristics, it may be useful to use a mock object in its place:
- supplies non-deterministic results (e.g. the current time or the current temperature);
- has states that are difficult to create or reproduce (e.g. a network error);
- is slow (e.g. a complete database, which would have to be initialized before the test);
- does not yet exist or may change behavior;
- would have to include information and methods exclusively for testing purposes (and not for its actual task).
For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.