Kiwi MVC.NET : Let’s go to TDD ! Part 4

So, now that I have introduced TDD in my Agile Page. I will try to use it along all the development of Kiwi.

First, I add to my Test project, 3 folders.

  • ControllerTest : where I will put all my tests related with a controller.
  • TestRepositories : there I will create fake repositories, to know if my service will work with other repositories.
  • IntegrationTest : there I will put all my tests related with the repositories. It will contain some “Spike Test”.

tdd_projects

Maybe, I will change this structure in the future.

For this example, you need to have the model Post. This model have some properties and one lazylist of PostLocalized.


public class Post
{
public int PostID { get; set; }
public string Author { get; set; }
public string Status { get; set; }
public DateTime Date { get; set; }
public DateTime ModifiedDate { get; set; }
public string Version { get; set; }
public string Permalink { get; set; }
public string Password { get; set; }
public LazyList<PostLocalized> LocalizedPost { get; set; }
}
public class PostLocalized
{
public string Title { get; set; }
public string Text { get; set; }
public string Excerpt { get; set; }
public string LanguageCode { get; set; }
}

Back to the code, I want to create a test which return a list of post form the DB. I created a class SqlCMSRepositoryTest in my IntegrationTest folder.If I write the code in VS (like you can see in the picture here under) VS says me that the function GetAll() does not exsit. Without executing the test, I know that we have to create this function. Normally in a pure TDD way, you should try to rebuild and see if it succeed.  Of course, you will get an error. So, let’s correct it.

kiwi_part4_0

To correct it, I create a function GetPosts in the SqlCMSRepositories. Here is the code :

using System.Collections.Generic;
using System.Linq;
using CMS.MVC.Data.SqlRepository;

namespace CMS.MVC.Data
{
public class SqlCMSRespository
{
//the name we changed in the properties of our Linq to Sql class
private DB _db;

//constructor
#region .ctor

//we instanciate the db when we instantciate the SqlCMSRespository
//default constructor
public SqlCMSRespository()
{
_db = new DB();
}

public SqlCMSRespository(DB dataContext)
{
//override the current context with the on passed in
//for TDD
_db = dataContext;
}

#endregion

#region Post

public IQueryable<Post> getPosts()
{
var res = new List<Post>();
return res.AsQueryable();
}

#endregion
}
}

When I try to build I receive error, indeed, I instantiated the SqlCMSRepositoryTest in place of the SqlCMSRepository. Also, I should add a reference to the data library and decorate the class with the attribute [TestClass]. Oh it is  a lot of mistakes, isn’t it ?

After correcting the code, I build the solution and I execute the test with this code :


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CMS.MVC.Data;

namespace CMS.MVC.Web.Tests
{
[TestClass]
public class SqlCMSRepositoryTest
{
private SqlCMSRespository _repository;

/// <summary>
/// Loading the SqlCMSRepository each time we use the SqlCMSRespositoryTest
/// </summary>
[TestInitialize]
public void Setup()
{
_repository = new SqlCMSRespository();
}

[TestMethod]
public void can_get_all_post()
{
List<Post> PostList = _repository.getPosts().ToList();
Assert.IsNotNull(PostList);
}

}
}

The test passes.

kiwi_part4_2

So, now I go beyond, I will create another test method. I would like to do the same test but count if there is 2 posts in the db post.

But my db is empty, so before, I write a test “can_create_post”.

[TestMethod]
public void can_create_post()
{
Post thisPost = new Post();
thisPost.Author = "dervalp";
thisPost.Date = DateTime.Now;
thisPost.ModifiedDate = DateTime.Now;
thisPost.Password = string.Empty;
thisPost.Permalink = string.Empty;
thisPost.Version = string.Empty;
thisPost.Status = "Post";

PostLocalized PostContentEN = new PostLocalized();
PostContentEN.Excerpt = "This is a resum";
PostContentEN.LanguageCode = "EN";
PostContentEN.Title = "Titre du post";
PostContentEN.Text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
var ListLocalizedPost = new LazyList<PostLocalized>();
ListLocalizedPost.Add(PostContentEN);
thisPost.LocalizedPost = ListLocalizedPost;

//create the post in the db
_repository.Add(thisPost);

//It the application throw an exception the test will fail
Assert.IsNotNull(thisPost);

}

“thisPost” is never null but if it will throw you an exception if your test fails. So, I rebuild and execute the test. It fails.

To succeed the test, I add the function, Add() in my SqlCMSRespository :

public void Add(Post post)
{

}

So now, I execute the test and It succeeds. As I am stupid, I will create another test. I will make the have_post_one_item_in_db.


[TestMethod]
public void have_post_one_item_in_db()
{
var res = _repository.getPosts();

Assert.AreEqual(1, res.Count());
}

Of course, it fails. So to make this test passes, I have to adapt my function add.

I think, you understand the idea now. I will update this post with a more complete SqlCMSRespository class.

Feedback is welcome.

Leave a Reply