可以这样用起订量来嘲笑? [英] Can this be mocked with Moq?

查看:175
本文介绍了可以这样用起订量来嘲笑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的嘲讽一些外部依赖关系和我有与需要它的构造另一个第三方类的一个实例都有一个第三方类的麻烦。希望多所社区可以给我一些指导。

我要创建 SomeRelatedLibraryClass 的,它接受它的构造函数 SomeLibraryClass 的模拟实例模拟实例。我如何可以模拟 SomeRelatedLibraryClass 这样?

回购code ...

下面是我用在我的测试控制台应用程序中的主要方法。

 公共静态无效的主要()
{
    尝试
    {
        SomeLibraryClass SLC =新SomeLibraryClass(直接向第三方);
        slc.WriteMessage(第三方信息);
        Console.WriteLine();

        MyClass的MC =新MyClass的(通过MyClass的);
        mc.WriteMessage(MyClass的信息);
        Console.WriteLine();

        模拟< MyClass的> mockMc =新的模拟< MyClass的>(模拟MyClass的);
        mockMc.Setup(I => i.WriteMessage(It.IsAny<字符串>()))
            .Callback((字符串消息)=> Console.WriteLine(string.Concat(模拟SomeLibraryClass的WriteMessage:,消息)));

        mockMc.Object.WriteMessage(模拟信息);
        Console.WriteLine();
    }
    赶上(例外五)
    {
        字符串错误=的String.Format(在执行片断--- \ n以下错误发生:\ N {0} \ñ---,e.ToString());
        Console.WriteLine(错误);
    }
    最后
    {
        Console.Write(preSS任意键继续......);
        Console.ReadKey();
    }
}
 

下面是我用来包装一个第三方类,并使其成为Moq'd类:

 公共类MyClass的
{
    私人SomeLibraryClass _SLC;

    公共MyClass的(字符串constructMsg)
    {
        _SLC =新SomeLibraryClass(constructMsg);
    }

    公共虚拟无效的WriteMessage(字符串消息)
    {
        _SLC.WriteMessage(消息);
    }
}
 

下面是第三方类,我有工作的两个例子(您不能编辑这两):

 公共类SomeLibraryClass
{
    公共SomeLibraryClass(字符串constructMsg)
    {
        Console.WriteLine(string.Concat(SomeLibraryClass构造函数:constructMsg));
    }

    公共无效的WriteMessage(字符串消息)
    {
        Console.WriteLine(string.Concat(SomeLibraryClass的WriteMessage:,消息));
    }
}

公共类SomeRelatedLibraryClass
{
    公共SomeRelatedLibraryClass(SomeLibraryClass SLC)
    {
        //没做什么
    }

    公共无效的WriteMessage(字符串消息)
    {
        Console.WriteLine(string.Concat(SomeRelatedLibraryClass的WriteMessage:,消息));
    }
}
 

解决方案

AFAIK,如果你想模拟出的类不是虚或接口 - 你不能用起订量嘲笑它。如果您的第三方库并没有实现自己的班,我觉得你的运气了。

I am working on mocking some external dependencies and am having trouble with one 3rd party class that takes in it's constructor an instance of another 3rd party class. Hopefully the SO community can give me some direction.

I want to create a mock instance of SomeRelatedLibraryClass that takes in it's constructor a mock instance of SomeLibraryClass. How can I mock SomeRelatedLibraryClass this way?

The repo code...

Here is a Main method that I am using in my test console application.

public static void Main()
{
    try
    {
        SomeLibraryClass slc = new SomeLibraryClass("direct to 3rd party");
        slc.WriteMessage("3rd party message");
        Console.WriteLine();

        MyClass mc = new MyClass("through myclass");
        mc.WriteMessage("myclass message");
        Console.WriteLine();

        Mock<MyClass> mockMc = new Mock<MyClass>("mock myclass");
        mockMc.Setup(i => i.WriteMessage(It.IsAny<string>()))
            .Callback((string message) => Console.WriteLine(string.Concat("Mock SomeLibraryClass WriteMessage: ", message)));

        mockMc.Object.WriteMessage("mock message");
        Console.WriteLine();
    }
    catch (Exception e)
    {
        string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
        Console.WriteLine(error);
    }
    finally
    {
        Console.Write("Press any key to continue...");
        Console.ReadKey();
    }
}

Here is a class that I have used to wrap one third party class and allow it to be Moq'd:

public class MyClass
{
    private SomeLibraryClass _SLC;

    public MyClass(string constructMsg)
    {
        _SLC = new SomeLibraryClass(constructMsg);
    }

    public virtual void WriteMessage(string message)
    {
        _SLC.WriteMessage(message);
    }
}

Here are two examples of 3rd party classes I am working with (YOU CAN NOT EDIT THESE):

public class SomeLibraryClass
{
    public SomeLibraryClass(string constructMsg)
    {
        Console.WriteLine(string.Concat("SomeLibraryClass Constructor: ", constructMsg));
    }

    public void WriteMessage(string message)
    {
        Console.WriteLine(string.Concat("SomeLibraryClass WriteMessage: ", message));
    }
}

public class SomeRelatedLibraryClass
{
    public SomeRelatedLibraryClass(SomeLibraryClass slc)
    {
        //do nothing
    }

    public void WriteMessage(string message)
    {
        Console.WriteLine(string.Concat("SomeRelatedLibraryClass WriteMessage: ", message));
    }
}

解决方案

AFAIK, if the class you're trying to mock out isn't virtual or an interface - you can't mock it with Moq. If your 3rd party library doesn't implement their classes, I think you're out of luck.

这篇关于可以这样用起订量来嘲笑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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