创建模拟对象

发布于 2024-12-26 17:50:29 字数 707 浏览 0 评论 0原文

我在 WPF 中创建了简单的 GUI。我想展示一些从数据库获取的数据。但目前我只有 GUI 和几个可以对接收到的数据进行简单计算的函数。我知道我的目标是创建会生成一些“错误”数据的模拟对象,但我不知道如何开始。你能告诉我如何创建其中一个,然后我可以类似地创建其余的。这是我进行计算的类:

        public Statistic showUsersPostCount(Options options)
    {
        Query q = (Query)this.client.DoQuery();
        q.AddAuthor(options.Login);
        q.SetSinceDate(options.DateFrom);
        q.SetUntilDate(options.DateTo);
        q.AddTitleWord(options.Discussion);
        List<Entity> list = (List<Entity>)q.PerformQuery();

        Statistic statistic = new Statistic();

        statistic.UsersPostCount = list.Count;
        return statistic;
    }

该函数返回一些简单的统计数据。但我没有类 Query 的代码。我怎样才能模拟这个类的对象?

I created simple GUI in WPF. I would like to show there some data got from database. But for now I have only GUI and few functions that do simple calculations on received data. I know that my goal is to create mock objects that would generate some "false" data, but I have no idea how to start. Could you tell me how to create one of them, I could then create analogously the rest. Here is my class that does calculations:

        public Statistic showUsersPostCount(Options options)
    {
        Query q = (Query)this.client.DoQuery();
        q.AddAuthor(options.Login);
        q.SetSinceDate(options.DateFrom);
        q.SetUntilDate(options.DateTo);
        q.AddTitleWord(options.Discussion);
        List<Entity> list = (List<Entity>)q.PerformQuery();

        Statistic statistic = new Statistic();

        statistic.UsersPostCount = list.Count;
        return statistic;
    }

this functions returns some simple statistic. But I do not have code for class Query. How can I mock object of this class?

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

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

发布评论

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

评论(3

温柔女人霸气范 2025-01-02 17:50:29

使用提供的代码...您不能,为了模拟它,您需要某种方式为依赖项提供替代对象(在本例中是 .client 对象)。

事实上,该方法只有一个输入“选项”,但这对代码的影响相对较小。

此外,您声称要显示一个类的示例 - 但您没有 - 您只显示一个名为 showUsersPostCount 的方法。

With the code provided... You can't, in order to mock it, you need some way of providing an alternative object for the dependency (which in this case is the .client object).

As it is, that method has only one input 'options', but that has relatively minimal influence over the code there.

Additionally, you claim to be showing an example of a class - but you're not - you're only showing a method named showUsersPostCount.

九歌凝 2025-01-02 17:50:29

假设您的代码是您想要模拟的类中的一个方法,那么您的第一步将是为要实现的类创建一个接口(如果您尚未这样做)。

然后,您可以将您的接口(而不是具体的类)传递给模拟框架(我使用了 Moq,但我认为 nmock 的工作方式非常相似)。然后,您可以填写您希望属性/方法通过模拟框架返回的模拟数据。

Assuming that your code is a method within a class that you want to mock, your first step would be creating an interface for the class to implement, if you have not already done so.

You can then pass your interface (rather than your concrete class) to a mocking framework (I've used Moq, but I assume nmock works very similarly). You can then fill in the mock data that you want your properties/methods to return through the mocking framework.

暮倦 2025-01-02 17:50:29

正如其他人所提到的,您的代码不能按原样进行模拟......至少使用标准模拟工具是这样。总有 Moles,它以允许您“嘲笑不可模仿的东西”而自豪。 ”。鼹鼠可以让你按原样模拟该方法。

也就是说,如果您必须借助 Moles 来模拟您内部控制的事物(该工具实际上是为模拟外部依赖项(例如数据库和文件等)而设计的),那么您可能应该考虑使您的设计更加灵活。总体而言,可测试的(也就是说,可以在没有摩尔的情况下进行测试)设计更有可能是一个好的设计。

As others have mentioned, your code isn't mockable as is... at least with standard mocking tools. There is always Moles, which prides itself on allowing you to "Mock the Unmockable". Moles would allow you to mock that method, as-is.

That said, if you have to resort to Moles to mock things that you control internally (the tool was really designed for mocking external dependencies such as databases and files and whatnot), you should probably consider making your design more flexible. A testable (testable without Moles, that is) design is more likely to be a good design, on the whole.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文