Schubert's Blog

A biker and technology enthusiast's
view of the world

Mapze.com and TDD & Mocks Part 2

In this blog entry I present two of the four test cases I've used to test the base business logic class from Mapze.com Beta 1. I've used the NUnit testing framework, as it was the simplest to implement with Visual Web Developer 2008. And Rhino Mock to pass in fake anonymous and logged in users to the base class. The code for the base class being tested here, is covered in the previous blog, so head over there if you haven't read it yet. On with the testing, first create fake accounts:

[TestFixture]
public class BlBaseText
{
//Faking a normal user account
Mock<IIdentity> FakeLoggedInSpereIIdentity;
//Faking an admin account
  Mock<IIdentity> FakeLoggedInMapzeAdminIIdentity;
//Faking an anonmymous account
  Mock<IIdentity> FakeAnonIIdentity;
...

In the NUnit setup function, I initialze the fake accounts.

  ...
[TestFixtureSetUp] public void Setup() { //Setting up the user account SPere
FakeLoggedInSpereIIdentity = new Mock<IIdentity>(); FakeLoggedInSpereIIdentity.Setup(i => i.IsAuthenticated).Returns(true).Verifiable(); FakeLoggedInSpereIIdentity.Setup(i => i.Name).Returns("SPere").Verifiable(); //Setting up the admin account MapzeAdmin
FakeLoggedInMapzeAdminIIdentity = new Mock<IIdentity>(); FakeLoggedInMapzeAdminIIdentity.Setup(i => i.IsAuthenticated).Returns(true).Verifiable(); FakeLoggedInMapzeAdminIIdentity.Setup(i => i.Name).Returns("MapzeAdmin").Verifiable(); //Setting up the anonymous account
FakeAnonIIdentity = new Mock<IIdentity>(); FakeAnonIIdentity.Setup(i => i.IsAuthenticated).Returns(false).Verifiable(); FakeAnonIIdentity.Setup(i => i.Name).Returns("").Verifiable(); }
...

Now that we have fake accounts, we can go ahead and setup the acutal test. The four test cases are:

1. The user is authenticated and their username exist in the database. This is the normal scenario and should be the case 90% of the time.

  ...
[Test]
public void ConstructorTest_UserIsAuthenticateAndValidUserName() { //Inject the fake user SPere
BlBase.FakeIIdentity = FakeLoggedInSpereIIdentity.Object; //The MVC state dictionary to store error and messages
ModelStateDictionary dictionary = new ModelStateDictionary(); //Initialise the base business logic class
BlBase blbase = new BlBase(dictionary, new L2SUserService()); //Make sure the current user variable is an instance of the IUser interface
Assert.IsInstanceOf<IUser>(blbase.currentUser); //Make sure the current user id is 1
Assert.AreEqual(1, blbase.currentUser.Id); //Make sure the user name is "SPere"
Assert.AreEqual("SPere", blbase.currentUser.UserName); //And finally make sure there were no errors or messages
Assert.AreEqual(0, dictionary.Count); }
...

2. The user is authenticated but the username does not exist in the database. Most likely cause of this is the user delete their account, but their authentication cookie is still valid as they were not logged out.

  ...
[Test] public void ConstructorTest_IsAuthenticateAndNotValidUserName() { //Create a fake account "NotValidUsername"
var FakeIIdentity = new Mock<IIdentity>();
FakeIIdentity.Setup(i => i.IsAuthenticated).Returns(true).Verifiable();
FakeIIdentity.Setup(i => i.Name).Returns("NotValidUserName").Verifiable();

//Inject the fake user "NotValidUsername"
BlBase.FakeIIdentity = FakeIIdentity.Object;
//The MVC state dictionary to store error and messages
ModelStateDictionary dictionary = new ModelStateDictionary();

//Initialise the base business logic class
BlBase blbase = new BlBase(dictionary, new L2SUserService());

//Make sure the current user variable is null
Assert.IsNull(blbase.currentUser);
//Make sure there is an error in the model state dictionary
Assert.AreEqual(1, dictionary.Count);
//Make sure the Severe Error key is present in the dictionary
Assert.AreEqual("SevereError", dictionary.Keys.FirstOrDefault());
//Make sure the message value is 1, so we can show generic severe error
//message and log the user out
Assert.AreEqual("1", dictionary["SevereError"].Errors.First().ErrorMessage);
}
}

To prevent this blog from turning into the longest blog post on the internet, I've not posted test 3 and 4, but they are similar. 3. was to check the scenario when the user is authenticated but is suspended and 4. is the scenario when the user is not authenticated. Hope this helps someone. As always, I'm open to suggestion and sharing best practices when it comes to testing, coding, designing and motorbiking Laughing.

Add comment




biuquote
Loading