Posts Tagged Repository
BDD with SpecFlow, NUnit and MVC3–Getting Started
Up to now I have been using Test Driven Development (and occasionally just Unit Testing after the fact) but I want to start using Behaviour Driven Development as it seems a more natural way to flesh out an application.
When using TDD, code is tested one unit (class or method) at a time, any dependencies are mocked, faked, or stubbed out, so any errors are within the unit being tested. With BDD testing is done one feature at a time from the outside in, starting at or near the interface and progressing through further tests down toward the database.
With MVC there is a choice of starting with the interface using a framework such as Watin or just starting with the controller. I intend to start with the controller, if I need to test routing I will do so separately. At the other end I will test down as far as the repository, faking the database context.
Getting Set Up
I’m using Visual Studio 2010 so will take advantage of the Nuget package manager. I have also decided to use SpecFlow as my BDD framework of choice, I am sure other frameworks are equally as good. Whilst there is a package for SpecFlow, it doesn’t seem to add all it should at the moment (VS templates), so it is worth downloading and running the installer from http://specflow.org/.
Now create a new MVC3 razor project in Visual Studio (no test project), then create a second, class assembly, project in the solution named Specs. Right click the Specs project and select Add Library Package Reference. From the Nuget dialog install:
- SpecFlow
- NUnit
- Moq
- Should
- ShouldFluent
SpecFlow can use most of the testing frameworks, my preference is for NUnit. I have not used Should before but it looks a great way to write more readable (therefore more maintainable) tests.
In the next post I’ll start my first feature (view a list of products).
Entity Classes, IDs and Equality
Posted by John in Methodology on April 22, 2010
I want my entity classes to rely on their ID when checking equality, the ID is populated from the repository when I retrieve an object and automatically generated when I save a new entity.
This poses the problem of how I compare two entities before they have been saved while working with them in my domain, or, when I have ‘newed’ them up myself for unit testing. The answer is to use a transient ID.
Normally an Entity Base class might look like this:
public abstract class EntityBase : IEquatable<EntityBase> {
public virtual int Id { get; protected set; }
public override bool Equals(object other) {
return Equals(other as EntityBase);
}
public override int GetHashCode() {
return Id;
}
public virtual bool Equals(EntityBase other) {
if (other == null) return false;
return other.Id == Id && other.GetType() == GetType();
}
}
If two new objects are created from a derived class then object1.Equals(object2) will be true as Id will be 0 for both objects. To avoid this I need to check if an object has not yet been saved:
private bool IsTransient {
get { return Id == 0; }
}
And just check for referential equality if this is the case:
public virtual bool Equals(EntityBase other) {
if (other == null) return false;
if (IsTransient) return ReferenceEquals(this, other);
return other.Id == Id && other.GetType() == GetType();
}
GetHashCode() needs to be overridden to reflect this:
public override int GetHashCode() {
if (IsTransient) return base.GetHashCode();
return Id;
}
The full code for EntityBase is:
public abstract class EntityBase : IEquatable<EntityBase> {
public virtual int Id { get; protected set; }
private bool IsTransient {
get { return Id == 0; }
}
public override bool Equals(object other) {
return Equals(other as EntityBase);
}
public override int GetHashCode() {
if (IsTransient) return base.GetHashCode();
return Id;
}
public virtual bool Equals(EntityBase other) {
if (other == null) return false;
if (IsTransient) return ReferenceEquals(this, other);
return other.Id == Id && other.GetType() == GetType();
}
}
Installing Subversion on Windows
My requirements are to host the repository on a windows machine, mostly for access from the LAN but also to be accessible over the Internet. The server is running Vista but the install and set up process should be similar on other versions of windows.
Currently there are two relevant builds of Subversion, one based on apache 2.0 and one based on 2.2. I am going to install Apache 2.2 and the corresponding Subversion build.
Install the Apache HTTP server.
Download and run the latest subversion build (currently svn-1.4.6-setup.exe) from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=8100&expandFolder=8100&folderID=91
Copy the file mod_dav_svn.so from C:\Program Files\Subversion\bin to C:\Program Files\Apache Software Foundation\Apache2.2\modules.
Open the Apache configuration file (‘Start > All Programs > Apache HTTP Server 2.2 > Configure Apache Server > Edit the Apache httpd.conf Configuration File’) and find the line ‘#LoadModule dav_module modules/mod_dav.so’.
Remove the ‘#’ from the front of that line then add the line ‘LoadModule dav_svn_module modules/mod_dav_svn.so’ below it. Save the file and restart the service to confirm everything is working as it should.
Create the directory where you want to store the repositories (e.g. D:\Svn).
Go back to the Apache configuration file and at the end of the file add:
<Location /Svn>
DAV svn
SVNListParentPath on
SVNParentPath D:\Svn
</Location>
Save the changes then restart the Apache service for them to take effect.
You should now be able to browse to http://localhost:81/Svn/ and see a page with zero repositories.