Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best create a test DB when doing TDD?

what's the best practice for creating test persistence layers when doing an ASP.NET site (eg. ASP.NET MVC site)?

Many examples I've seen use Moq (or another mocking framework) in the unit test project, but I want to, like .. moq out my persistence layer so that my website shows data and stuff, but it's not coming from a database. I want to do that last. All the mocking stuff I've seen only exists in unit tests.

What practices do people do when they want to (stub?) fake out a persistence layer for quick and fast development? I use Dependency Injection to handle it and have some hard coded results for my persistence layer (which is really manual and boring).

What are other people doing? Examples and links would be awesome :)

UPDATE

Just a little update: so far I'm getting a fair bit of mileage out of having a fake repository and a SQL repository - where each class implements an interface. Then, using DI (I'm using StructureMap), I can switch between my fake repository or the SQL repository. So far, it's working well :)

(also scary to think that I asked this question nearly 11 months ago, from when I'm editing this, right now!)

like image 812
Pure.Krome Avatar asked Nov 13 '08 13:11

Pure.Krome


People also ask

How do you create a test DB?

In the ClearQuest Schema Repository Explorer view, click on the schema repository where you want to add the test database. Click View > ClearQuest Database Admin. The ClearQuest Database Admin view opens. In the ClearQuest Database Admin view, click the Create Database button.

What is a test management tool used for in TDD?

Test-driven development (TDD) is a development technique where the developer must first write a test that fails before writing a new functional code. TDD ensures a proven way to ensure effective unit testing; however, it does not replace traditional testing.


1 Answers

Assuming you're using the Repository pattern from Rob Conery's MVC Store Front:

http://blog.wekeroad.com/mvc-storefront/mvc-storefront-part-1/

I followed Rob Conery's tutorial but ran into the same want as you. Best thing to do is move the Mock Repositories you've created into a seperate project called Mocks then you can swap them out pretty easily with the real ones when you instantiate your service. If your feeling adventurous you could create a factory that takes a value from the config file to instantiate either a mock or a real repository,

e.g.

public static ICatalogRepository GetCatalogRepository(bool useMock)
{
     if(useMock)
          return new FakeCatalogRepository();
     else
          return new SqlCatalogRepository();
}

or use a dependency injection framework :)

container.Resolve<ICatalogRepository>();

Good luck!

EDIT: In response to your comments, sounds like you want to use a list and LINQ to emulate a db's operations e.g. GetProducts, StoreProduct. I've done this before. Here's an example:

public class Product
{
     public int Identity { get; set; }
     public string Name { get; set; }
     public string Description { get; set; }
     //etc
}

public class FakeCatalogRepository()
{
     private List<Product> _fakes;

     public FakeCatalogCatalogRepository()
     {
          _fakes = new List<Product>();

          //Set up some initial fake data
          for(int i=0; i < 5; i++)
          {
              Product p = new Product
              {
                 Identity = i,
                 Name = "product"+i,
                 Description = "description of product"+i
              };

              _fakes.Add(p);
          }
     }

     public void StoreProduct(Product p)
     {
         //Emulate insert/update functionality

         _fakes.Add(p);
     }

     public Product GetProductByIdentity(int id)
     {
          //emulate "SELECT * FROM products WHERE id = 1234
          var aProduct = (from p in _fakes.AsQueryable()
                         where p.Identity = id
                         select p).SingleOrDefault();

          return aProduct;
     }
}

Does that make a bit more sense?

like image 97
Rob Stevenson-Leggett Avatar answered Sep 30 '22 17:09

Rob Stevenson-Leggett