English 中文(简体)
1. 制造 mo物体
原标题:Creating mock objects

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?

问题回答

该守则提供了...... 为了改变这种倾向,你需要某种方式为依赖性提供替代物(在这种情况下是客户目标)。

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.

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.

然后,你可以把你的接口(而不是你的具体类别)移至一个模拟框架(我使用的是Moq,但我也认为ock工作非常相似)。 然后,你可以填写你希望通过模拟框架归还其财产/方法的模拟数据。

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.

尽管如此,如果你不得不利用摩苏尔对你内部控制的东西进行模拟(这一工具实际上是为篡改数据库和档案等外部依赖物而设计的),你可能应考虑使你的设计更加灵活。 一种可检测的(如无摩苏尔可检测的)设计更可能是一种良好的设计。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签