我是否可以为不同技术的服务层单元测试创​​建通用基础架构 [英] Can i create a common infrastructure for unit testing the service layer in different technologies

查看:45
本文介绍了我是否可以为不同技术的服务层单元测试创​​建通用基础架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的asp.net Web服务服务层,它没有进行任何类型的单元测试。我的经理决定引入单元测试层来测试项目。并排整个Web服务正在转向更新的技术,即asp.net web api。我想创建一个通用的基础架构,以后可以用于单元测试web apis。那可能吗?如果是,我该如何处理呢?



我尝试过:



我不知道这是否可以实现。我曾尝试在互联网上搜索一些线索,但找不到任何东西。

I have an existing asp.net web services service layer which does not have any sort of unit testing happening. My manager has decide to introduce a unit testing layer to test the projects. Side by side the whole web service is moving to a newer technology i.e. asp.net web api. I want to create a common infratructure which can later be used for unit testing web apis as well. Is that possible? If yes, how can I proceed with this?

What I have tried:

I do not have any idea if this can be achieved. I have tried searching on internet for some clue but could not find anything.

推荐答案

首先,您需要了解单元测试是什么。单元测试是指在测试逻辑时,不使用数据库,服务或类似的东西,它只测试逻辑。如果项目没有被编写为可单元测试,那么它可测试的可能性几乎为零,并且需要大量重构。假设你有一个带转移方法的银行网络服务



First of all you need to understand what unit testing is. Unit testing is when you test the logic, you don't use databases, services or anything like that, it only tests the logic. If the project is not written to be unit testable then the chances of it being testable are almost zero and will require a lot of refactoring. Let's say you have a bank web service with a Transfer method

[WebMethod]
public bool Transfer(decimal amount, string sourceAccount, string destinationAccount)
{
    // code here that accesses the database
    // and executes SQL to remove from one account
    // and add to another

    return true;
}





这可能是你现在所拥有的,所以你需要做的是创建一个处理逻辑和处理数据访问的类。





This is probably what you have at the moment so what you need to do is create a class that handles the logic and a class that handles the data access.

public interface IBank
{
    bool Subtract(decimal amount, string account);
    bool Add(decimal amount, string account);
}







public class Bank : IBank
{
    public bool Subtract(decimal amount, string account)
    {
        // your data access code will go here

        return true;
    }

    public bool Add(decimal amount, string account)
    {
        // your data access code will go here

        return true;
    }
}





具有业务逻辑的课程





And the class with the business logic

public class BankService
{
    private IBank bank;

    // you'll probably use an IOC container for this
    public BankService() : this(new Bank())
    {
    }

    public BankService(IBank bank)
    {
        this.bank = bank;
    }

    public bool Transfer(decimal amount, string sourceAccount, string destinationAccount)
    {
        this.bank.Subtract(amount, sourceAccount);
        this.bank.Add(amount, destinationAccount);

        return true;
    }
}





这是本课程和本课程,我们只进行单元测试。更新您的Web方法以使用此类而不是在web方法中包含代码





It is this class and this class only we'll be unit testing. Update your web method to use this class instead of having the code inside the webmethod

[WebMethod]
public bool Transfer(decimal amount, string sourceAccount, string destinationAccount)
{
    BankService bs = new BankService();
            
    return bs.Transfer(amount, sourceAccount, destinationAccount);
}





因此webmethod使用BankService,这就是传输逻辑,而BankService使用Bank来执行数据库更新。我们需要测试的逻辑是,当转移资金时,钱从一个减去并添加到另一个,所以我们将模拟IBank类(我在这个例子中使用moq),这样我们就不需要数据库并且没有数据库访问权限,我们将测试是否已调用相应的方法,因此单元测试将如下所示





So the webmethod uses BankService and that is where the transfer logic is, and that BankService uses Bank to do the database updates. The logic we need to test is that when transferring money the money is subtracted from one and added to the other, so we'll mock the IBank class (I'm using moq in this example) so that we don't need the database and there is no database access and we'll test that the appropriate methods have been called, so the unit test will look like this

Mock<IBank> bank = new Mock<IBank>();

BankService bankService = new BankService(bank.Object);
bankService.Transfer(10, "111", "222");

bank.Verify(b => b.Subtract(10, "111"));
bank.Verify(b => b.Add(10, "222"));





这是你需要做的基础知识,你需要将事情分解为执行专用任务的类,并让这些任务实现接口,以便可以在任何级别模拟它们。实际上,你的BankService类本身会实现IBankService,以防你需要模拟整个事情。



That's the basics of what it is you need to do, you need to break things into classes that do dedicated tasks and have those tasks implement interfaces so that they can be mocked at any level. In reality your BankService class itself would implement IBankService in case you needed to mock the entire thing.


这篇关于我是否可以为不同技术的服务层单元测试创​​建通用基础架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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