什么是路要走假冒我的数据库层中的单元测试? [英] What is the way to go to fake my database layer in a unit test?

查看:180
本文介绍了什么是路要走假冒我的数据库层中的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试的问题。

I have a question about unit testing.

说我有一个控制器上创建方法,提出一个新的客户数据库中的:

Say I have a controller with one create method which puts a new customer in the database:

//code a bit shortened
public actionresult Create(Formcollection formcollection){
    client c = nwe client();
    c.Name = formcollection["name"];
    ClientService.Save(c);
{

Clientservice会召唤一个datalayer对象,并将其保存在数据库中。

Clientservice would call a datalayer object and save it in the database.

我现在要做的就是创建一个数据库testscript并设置我的数据库在测试前一个知道病情。 所以,当我测试这个方法在单元测试中,我知道必须有一个多客户端数据库,以及其is.In简称:

What I do now is create a database testscript and set my database in a know condition before testing. So when I test this method in the unit test, I know that there must be one more client in the database, and what its name is.In short:

ClientController cc = new ClientController();
cc.Create(new FormCollection (){name="John"});
//i know i had 10 clients before
assert.areEqual(11, ClientService.GetNumberOfClients());
//the last inserted one is John
assert.areEqual("John", ClientService.GetAllClients()[10].Name);

所以,我读过,单元测试不应该打的数据库,我已经成立了一个国际奥委会的数据库类,但是然后呢? 我可以创建一个假的数据库类,并使其什么也不做。

So I've read that unit testing should not be hitting the database, I've set up an IOC for the database classes, but then what? I can create a fake database class, and make it do nothing.

不过,那当然我的说法是行不通的,因为如果我说 GetNumberOfClients()它总是会返回X,因为它具有与所使用的假数据库类不存在交互作用创建方法。

But then of course my assertions will not work because if I say GetNumberOfClients() it will always return X because it has no interaction with the fake database class used in the Create Method.

我还可以在假的数据库类创建客户名单,但一样会有创建了两个不同的实例(一个在控制器动作,一个在单元测试),他们将没有任何互动。

I can also create a List of Clients in the fake database class, but as there will be two different instances created (one in the controller action and one in the unit test), they will have no interaction.

是什么让这个单元测试工作,而数据库的方式吗?

What is the way to make this unit test work without a database?

编辑: 该clientservice不直接连接到数据库。它调用一个ClientDataClass将连接到数据库。所以ClientDatabaseClass将被替换假

The clientservice doesn't connect directly to the DB. It calls a ClientDataClass which will connect to the database. So the ClientDatabaseClass will be replaced with a fake

推荐答案

在您从数据库测试控制器隔离这种特殊情况下。 ClientService是抽象在数据库中,并且应该由测试双代替。您注射假到控制器,但仍然断言真正实施。这是没有意义的。

In this particular case you are testing controller in isolation from database. ClientService is abstraction over the database, and should be replaced by test double. You injected fake into controller, but still assert real implementation. This makes no sense.

断言相同的对象,将其注入到控制器。

Assert the same object, which was injected into controller.

 interface IClientService 
    {
      public void GetNumberOfClients();
      public IList<Client> GetAllClients();
      public void Insert(Client client);
    }

假服务的实现:

Fake service implementation:

   class FakeClientService : IClientService
   {
     private IList<CLient> rows = new List<CLient>();

     public void GetNumberOfClients()
     { 
       return list.Count;
     }

     public IList<Client> GetAllClients()
     {
       return list;
     }

     public void Insert(Client client)
     {
       client.Add(client);
     }
   }

测试:

[Test]
public void ClientIsInserted()
{
  ClientController cc = new ClientController();
  FakeClientService fakeService = new FakeClientService();

  cc.ClientService = fakeService;

  cc.Create(new FormCollection (){name="John"});

  assert.areEqual(1, fakeService.GetNumberOfClients());
  assert.areEqual("John", fakeService.GetAllClients()[0].Name);
}

制造假的ClientDatabaseClass - 如果您想查询如何控制和服务协同工作

。这将是这样的:

If you want to check how controller and service work together - create fake for ClientDatabaseClass. It would be like:

[Test]
public void ClientIsInserted()
{
  ClientController cc = new ClientController();
  IClientDatabaseClass databaseFake = new ClientDatabaseClassFake();

  ClientService service= new ClientService();

  service.Database = databaseFake;
  cc.ClientService = service;

  cc.Create(new FormCollection (){name="John"});

  assert.areEqual(1, service.GetNumberOfClients());
  assert.areEqual("John", service.GetAllClients()[0].Name);
}

这篇关于什么是路要走假冒我的数据库层中的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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