Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将接口抽象到具有扩展方法的类_C#_Visual Studio_Unit Testing_Nunit - Fatal编程技术网

C# 将接口抽象到具有扩展方法的类

C# 将接口抽象到具有扩展方法的类,c#,visual-studio,unit-testing,nunit,C#,Visual Studio,Unit Testing,Nunit,如果我在一个具体的类中使用扩展方法,为了单元测试的目的,我如何用它的接口替换该类 我有一个方法: [HttpGet] [Route("/yoyo/{yoyoId:int}/accounts")] public ResponseYoyoEnvelope GetAccountsByYoyoId([FromBody] RequestYoyoEnvelope requestYoyoEnvelope, int yoyoId) { var responseYoyoEnvelope = request

如果我在一个具体的类中使用扩展方法,为了单元测试的目的,我如何用它的接口替换该类

我有一个方法:

[HttpGet]
[Route("/yoyo/{yoyoId:int}/accounts")]
public ResponseYoyoEnvelope GetAccountsByYoyoId([FromBody] RequestYoyoEnvelope requestYoyoEnvelope, int yoyoId)
{
    var responseYoyoEnvelope = requestYoyoEnvelope.ToResponseYoyoEnvelope();

    // get our list of accounts
    // responseEnvelope.Data = //list of accounts
    return responseYoyoEnvelope;
}
我想替换:

RequestYoyoEnvelope requestYoyoEnvelope
抽象地说:

IRequestYoyoEnvelope requestYoyoEnvelope
但是,
toresponseyoenvelop
是一种扩展方法

如果我在一个具体的类中使用扩展方法,为了单元测试的目的,我如何用它的接口替换该类?

public class RequestYoyoEnvelope : IRequestYoyoEnvelope { ... }
您的扩展方法需要以接口为目标

public static ResponseYoyoEnvelope ToResponseYoyoEnvelope(this IRequestYoyoEnvelope target) { ... }
保持操作不变,因为模型绑定器绑定接口时会出现问题

在单元测试中,您通过了
requestyoenvelope
的具体实现,并且应该能够测试更新的扩展方法

从您的示例中,您不需要一个接口来测试该方法是否为正在测试的方法。只需新建模型的一个实例,并在单元测试期间将其传递给方法

[TestMethod]
public void GetAccountsByYoyoIdTest() {
    //Arrange
    var controller = new YoyoController();
    var yoyoId = 123456;
    var model = new RequestYoyoEnvelope {
        //you populate properties for test
    };
    //Act
    var result = controller.GetAccountsByYoyoId(model, yoyoId);

    //Assert
    //...do your assertions.
}
假定

public class RequestYoyoEnvelope : IRequestYoyoEnvelope { ... }
您的扩展方法需要以接口为目标

public static ResponseYoyoEnvelope ToResponseYoyoEnvelope(this IRequestYoyoEnvelope target) { ... }
保持操作不变,因为模型绑定器绑定接口时会出现问题

在单元测试中,您通过了
requestyoenvelope
的具体实现,并且应该能够测试更新的扩展方法

从您的示例中,您不需要一个接口来测试该方法是否为正在测试的方法。只需在单元测试期间更新模型实例并将其传递给方法

[TestMethod]
public void GetAccountsByYoyoIdTest() {
    //Arrange
    var controller = new YoyoController();
    var yoyoId = 123456;
    var model = new RequestYoyoEnvelope {
        //you populate properties for test
    };
    //Act
    var result = controller.GetAccountsByYoyoId(model, yoyoId);

    //Assert
    //...do your assertions.
}

您可以针对接口而不是具体类编写扩展方法:

public static class Class2
{
    public static void Extension(this ITestInterface test)
    {
        Console.Out.WriteLine("This is allowed");
    }
}
然后你可以做:

// "Test" is some class that implements the ITestInterface interface
ITestInterface useExtensionMethod = new Test();
useExtensionMethod.Extension();
还要注意的是,即使
useExtensionMethod
不是显式的
ITestInterface
类型,该方法仍然有效:

Test useExtensionMethod = new Test();
useExtensionMethod.Extension();

这是否代表了一种装饰模式,但至少要记住,扩展方法并不是界面本身的一部分——“在引擎盖下,”只是编译器允许您方便地将其视为实例方法。

您可以针对接口而不是具体类编写扩展方法:

public static class Class2
{
    public static void Extension(this ITestInterface test)
    {
        Console.Out.WriteLine("This is allowed");
    }
}
然后你可以做:

// "Test" is some class that implements the ITestInterface interface
ITestInterface useExtensionMethod = new Test();
useExtensionMethod.Extension();
还要注意的是,即使
useExtensionMethod
不是显式的
ITestInterface
类型,该方法仍然有效:

Test useExtensionMethod = new Test();
useExtensionMethod.Extension();

这是否代表了一种装饰模式,但至少要记住,扩展方法并不是界面本身的一部分——“在引擎盖下,”只是编译器允许您方便地将其视为实例方法。

当我意识到我遗漏了什么——这是一个API端点时,我调整了我的答案。然后我读了这个答案-@Nkosi没有漏掉它,这个答案更切中要害。当我意识到我漏掉了什么-这是一个API端点时,我调整了我的答案。然后我读了这个答案-@Nkosi没有错过它,这个答案更切题。