moqing静态方法调用c#库类

发布于 2024-08-24 13:48:52 字数 350 浏览 5 评论 0原文

这似乎是一个很简单的问题,但我似乎找不到关键字来影响我的搜索。

我试图通过模拟此方法调用中的所有对象来进行单元测试。我可以对我自己的所有创作执行此操作,除了这个:

public void MyFunc(MyVarClass myVar)
{
    Image picture;
    ...

    picture = Image.FromStream(new MemoryStream(myVar.ImageStream));

    ...
}

FromStream 是来自 Image 类(C# 的一部分)的静态调用。那么我如何重构我的代码来模拟它,因为我真的不想为单元测试提供图像流。

This seems like an easy enough issue but I can't seem to find the keywords to effect my searches.

I'm trying to unit test by mocking out all objects within this method call. I am able to do so to all of my own creations except for this one:

public void MyFunc(MyVarClass myVar)
{
    Image picture;
    ...

    picture = Image.FromStream(new MemoryStream(myVar.ImageStream));

    ...
}

FromStream is a static call from the Image class (part of c#). So how can I refactor my code to mock this out because I really don't want to provide a image stream to the unit test.

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

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

发布评论

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

评论(4

撑一把青伞 2024-08-31 13:48:52

您可以创建一个 IImageLoader 接口。 “正常”实现仅调用 Image.FromStream,而您的单元测试版本可以执行您需要执行的任何操作。

You can create an IImageLoader interface. The "normal" implementation just calls Image.FromStream, while your unit test version can do whatever you need it to do.

静待花开 2024-08-31 13:48:52

Moq 和大多数其他模拟框架不支持模拟静态方法。但是, TypeMock 确实支持模拟静态方法,如果您愿意购买,您可能会感兴趣它。否则,您将不得不重构以便可以模拟接口......

Moq and most other mocking frameworks don't support mocking out static methods. However, TypeMock does support mocking out static methods and that might be of interest to you if you're willing to purchase it. Otherwise, you'll have to refactor so that an interface can be mocked...

轻许诺言 2024-08-31 13:48:52

您可以将静态函数包装到 Func 类型属性中,该属性可以通过使用模拟或存根的单元测试来设置。

public class MyClass
{
    ..

    public Func<Image, MemoryStream> ImageFromStream = 
                                     (stream) => Image.FromStream(stream);


    public void MyFunc(MyVarClass myVar)
    {
        ...

        picture = ImageFromStream(new MemoryStream(myVar.ImageStream));

        ...
    }


    ..
}

You could wrap the static function into Func type property which can be set by the unit test with a mock or stub.

public class MyClass
{
    ..

    public Func<Image, MemoryStream> ImageFromStream = 
                                     (stream) => Image.FromStream(stream);


    public void MyFunc(MyVarClass myVar)
    {
        ...

        picture = ImageFromStream(new MemoryStream(myVar.ImageStream));

        ...
    }


    ..
}
温柔戏命师 2024-08-31 13:48:52

这可以通过 Visual Studio 2010 强大工具 Moles 来实现。 Moles 代码如下所示:

// replace Image.FromStream(MemoryStream) with a delegate
MImage.FromStreamMemoryStream = stream => null; 

This can be acheived with Moles, a Visual Studio 2010 Power Tools. The Moles code would look like this:

// replace Image.FromStream(MemoryStream) with a delegate
MImage.FromStreamMemoryStream = stream => null; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文