使用 SQL Server CE 4 或 SQLite 与 Entity Framework 4 Code First 进行集成测试

发布于 2024-10-17 13:56:11 字数 1524 浏览 0 评论 0原文

我想从集成测试开始。我正在使用 ASP.NET MVC 3 应用程序。我正在使用 Entity Framework 4 Code First CTP5。我对数据库的集成测试位于一个单独的项目中,例如 MyProject.Data.IntegrationTests。

我计划使用 SQL Server CE 4 或 SQLite。关于使用其中任何一项来实现我想要完成的任务有什么建议/提示/意见吗?

有谁知道我可以阅读有关我想要实现的目标的任何不错的文章吗?如有帮助/反馈,我们将不胜感激。

我的数据库使用 SQL Server 2008。但是,在测试我的存储库时,我想针对上述数据库之一测试它们,因此我需要指定连接字符串。

更新

我从服务层工作(从我的控制器调用),然后服务层将调用我的存储库。例如,下面是我添加新闻项的方法:

服务类:

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;
   }
}

存储库类:

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;
   }
}

我正在使用 Entity Framework 4 Code First CTP5 和 NUnit。 NUnit 是否有类似于 XUnit 中的回滚功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-10-24 13:56:11

如果您使用像 XUnit (http://xunit.codeplex.com/) 这样的测试框架,它会具有名为 [AutoRollback] 的功能,该功能将回滚测试中运行的事务,因此您的任何数据都不会更改!

至于如何设置测试,我需要更多地了解如何设置数据访问。您使用了存储库模式吗? (实体框架 4 CTP 4 / CTP 5 通用存储库模式和可单元测试)。如果我能看到你的一些代码将会有所帮助。下面是与 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);
}

因此,上述测试将用户添加到我的数据库中,然后检查其 Id 属性以检查 SQL Server 是否自动为其生成了 Id。因为该方法用 AutoRollback 属性修饰,所以该方法结束后数据将从我的数据库中删除!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文