6

I would like to start with integration testing. I am using an ASP.NET MVC 3 app. And I am using Entity Framework 4 Code First CTP5. My integration tests to the database is in a separate project something like MyProject.Data.IntegrationTests.

I am planning on using SQL Server CE 4 or SQLite. Any recommendations/tips/opinions on using any one of these for what I am trying to accomplish?

Does anyone know of any decent articles that I can read on what I am trying to accomplish? And help/feedback would be appreciated.

I am using SQL Server 2008 for my database. But when testing my repositories I would like to test them against one of these database mentioned above, so I will need to specify the connection string.

UPDATE

I work from a service layer (called from my controller) and then the service layer will call my repository. For examples, below is how I would add a news item:

Service class:

public class NewsService : INewsService
{
   private INewsRepository newsRepository;

   public NewsService(INewsRepository newsRepository)
   {
      this.newsRepository = newsRepository;
   }

   public News Insert(News news)
   {
      // Insert news item
      News newNews = newsRepository.Insert(news);

      // Insert audit entry

      // Return the inserted news item's unique identifier
      return newNews;
   }
}

Repository class:

public class NewsRepository : INewsRepository
{
   MyContext context = new MyContext();

   public NewsRepository()
   {
   }

   public News Insert(News news)
   {
      int newsId = context.Database.SqlQuery<int>("News_Insert @Title, @Body, @Active",
         new SqlParameter("Title", news.Title),
         new SqlParameter("Body", news.Body),
         new SqlParameter("Active", news.Active)
      ).FirstOrDefault();

      news.NewsId = newsId;

      // Return the inserted news item
      return news;
   }
}

I am using Entity Framework 4 Code First CTP5 and NUnit. Does NUnit has something similar to the roll back in XUnit?

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234

1 Answers1

2

If you use a testing framework like XUnit (http://xunit.codeplex.com/), it comes with a feature called [AutoRollback] and that will rollback the transaction ran in the test so none of your data will change!

As far as how to setup the tests, I need to see more of how you setup your data access. Did you use the Repository Pattern? (Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable). If I could see some of your code it would help. Below is a sample integration test with XUnit:

private readonly IUserRepository _repository;

public UserRepositoryTests()
{
    _repository = new UserRepository(base._databaseFactory);
}
    
[Fact, AutoRollback]
public void Should_be_able_to_add_user()
{
    var user = new User{Name = "MockName"};
    _repository.Add(user);
    base._unitOfWork.Commit();
    
    Assert.True(user.Id > 0);
}

So the above test adds a user to my database, then checks its Id property to check that SQL Server auto generated an Id for it. Because the method is decorated with the AutoRollback attribute, the data is then removed from my database after the method ends!

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Paul
  • 12,392
  • 4
  • 48
  • 58
  • Thanks. Please see my updated post. Do you test with a live database? What type of database do you use? Do you have a separate project for this? Does your database reside in this project. Please let me know if you need nee to know anything else :) – Brendan Vogt Feb 17 '11 at 05:58
  • Had a look at your 1 post, don't you have a sample project that I can look at how you implemented things? – Brendan Vogt Feb 17 '11 at 06:13
  • @Brendan I don't have a sample site but would be happy to help you out. If your project is small, you can just add a new class library called "Tests". You would hold all your tests in that application and it would hold a reference to your MVC application (and the project that holds your dataaccess if not in your MVC app). With the AutoRollback attribute you can test agents your live dev database and it will revert everything to how it was before the test ran. I use MS SQL Server, but SQL CE will work the same! On a side note, I recommend Pro ASP.NET MVC 2 http://rdir.in/PROMVC2 – Paul Feb 17 '11 at 18:51
  • @Brendan You can check out the code for this project to see examples http://shrinkr.codeplex.com/ Note: This is not a simple app and may not be easy to understand if you are not familiar with DDD, S.O.L.I.D., CQS etc... – Paul Feb 17 '11 at 18:54
  • @Paul Your user`s Id is surely not a nullable integer. So your Assert does not make sense and will never fail. – Elisabeth Feb 22 '13 at 19:40