小巧玲珑:单元测试SQL查询 [英] Dapper: Unit Testing SQL Queries

查看:312
本文介绍了小巧玲珑:单元测试SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用短小精悍,微ORM,我用的是小巧精致的彩虹。我想测试的查询,并通过他们检索到的数据。

I am starting with Dapper, the micro-ORM, and i use the Dapper Rainbow. I want to test the queries and the data retrieved by them.

我的意思是,例如,我有 UserService的的方法 GETALL(),然后我想测试的SQL查询一些检索所有用户目录(而不是从数据库,因为我想测试要快)。你知不知道我该怎么办呢?

I mean, for example, i have the UserService with the method GetAll(), and i want to test that the sql query is retrieving all the users from some List (not from the database because i want the tests to be fast). Do you know how can i do that?

我的服务类(我想测试方法):

My service class (and the method i want to test):

public static class UserService{
    public static IEnumerable<User> GetAll(){
        return DB.Users.All();
    }
}

你有单元测试查询和数据检索什么建议?

Do you have any advice about unit testing queries and data retrieving?

感谢

推荐答案

我会建议对依赖注入和存储库模式阅读起来。如果你认为你在code中的上述做法,你将有一个很难嘲讽出相关性,因为类和方法是静态的。

I would suggest reading up on dependency injection and the repository pattern. If you take the approach that you have in the code above you will have a hard time mocking out the dependencies because the class and method is static.

下面是一个更好的办法。

Here is a better approach.

public interface IUserRepository
{
   IEnumerable<User> GetAll()
}

public class UserRepository : IUserRepository
{
  public IEnumerable<User> GetAll()
  { 
    return DB.Users.All();
  }
}

public class UserService
{
    IUserRepository _userRepository;
    public UserService(IUserRepository userRepository)
    {
      _userRepository = userRepository
    }

    public Enumerable<User> GetAll(){
        return _userRepository.GetAll();
    }
}

现在测试你可以模拟出你的资料库。我使用了一个名为NSubstitute嘲弄框架,它在我心目中是除了上面提到的其他人简单多了,但这是一个个人的preference。首先这里是你如何能没有任何嘲弄框架编写的测试。

Now for testing you can mock out your repository. I use a mocking framework called NSubstitute which in my mind is a lot simpler than the others mentioned above, but that is a personal preference. To start with here is how you could write your test without any mocking framework.

public class FakeUserRepository : IUserRepository
{
  public IEnumerable<User> GetAll()
  { 
    return new List<User> { new User {FirstName='Bob', LastName='Smith'}, };
  }
}

和测试中的

[Test]
public void GetAll_ShouldReturnAllFromFake()
{
   // Arrrange
   var userService = new UserService(new FakeUserRepository())
   // Act
   var result = userService.GetAll();
   // Assert
   var user = result[0];
   Assert.AreEqual("Bob", user.FirstName);
   Assert.AreEqual("Smith", user.LastName);   
}

这个例子是有点做作,因为它并没有真正意义的测试,你可以得到的数据从一个存储库假回。你会如何​​使用这个在现实中,如果你有这样的说回来一个用户,然后检查他们是早于特定年龄或东西在你的服务的一些业务逻辑。例如一个IsLegalDrivingAge方法上UserService的。

This example is a little contrived as it doesn't really make sense to test that you can get data back from a fake repository. How you would use this in reality is if you had some business logic in your service that say got back a user and then check that they were older than a certain age or something. e.g. an IsLegalDrivingAge method on UserService.

这篇关于小巧玲珑:单元测试SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆