0

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400

3 Answers3

0

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.

Arafangion
  • 11,517
  • 1
  • 40
  • 72
0

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.

Dan A.
  • 2,914
  • 18
  • 23
0

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.

Erik Dietrich
  • 6,080
  • 6
  • 26
  • 37