JustMock outside of unit testing?

2 posts, 0 answers
  1. TonyG
    TonyG avatar
    239 posts
    Member since:
    Feb 2006

    Posted 02 Nov 2011 Link to this post

    I confess up front that of all of the hundreds of skills I've had to learn over the years, at this point I'm still not up on unit testing.  I haven't used the tools once, haven't read a book or done my homework on this one, and don't know AAA from the company that tows my car.  In JustMock I think I see a tool that can help with common development.  I just don't want to have to research another paradigm like unit testing (yet) in order to use what seems like a great tool.  But all of the examples seem to be completely based on unit testing.

    Here is an example of how I'd like to use JustMock.  I have an ASP.NET app that makes synchronous WebRequests out to other sites to aggregate results.  My code might look something like this:

    IList<ClassA> GetGridData(string info) {
      IList<ClassA> list  = LibraryX.DoSomething(info);  // web request done in here
      if (list.Count == 0)
        list = null;
      return list;

    The problem is that I in testing through Visual Studio I don't have the live web server environment, and I don't want to have to do a live call through LibraryX to get a IList<ClassA> that I need elsewhere in my code.  It would be great if I could generate that list without having to write the manual code to generate N instances of ClassA.  The issue is compounded when ClassA is sealed and instances are constructed internally in LibraryX, and I actually can't manually create those instances anyway without a lot of grief.

    So can I do something like this?

    IList<ClassA> GetGridData(string info) {
    #if Testing
       IList<ClassA> list  = Mock.Create<IList<ClassA>>(info);
    // or....
       IList<ClassA> list = Mock.Create<LibraryX.ClassAFactory>().DoSomething(info);
      IList<ClassA> list  = LibraryX.ClassAFactory.DoSomething(info);  // web request done in here
      if (list.Count == 0)
        list = null;
      return list;

    As I see it, JustMock doesn't seem to have the tools it needs to create a mock list of ClassA objects ... or maybe that's exactly what JustMock is great at doing!?  I really don't know because I'm not "getting" this from any of the documentation.  Is there something else that does what I want here?  Or is the universal solution still to just lump it and do it all manually?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 03 Nov 2011 Link to this post

    Hi Tony,
    Thanks again for bringing up the question. Actually, if you have to get through the following code:
    IList<ClassA> GetGridData(string info) {
      IList<ClassA> list  = LibraryX.DoSomething(info);  // web request done 
      if (list.Count == 0)
        list = null;
      return list;

    Indeed, you can do something like this :

    Mock.Arrange(() => LibraryX.DoSomething(expectedInfo).ReturnsCollecton(myFakeList);

    Here when GetGridData(expectedInfo) is invoked with an expected argument it will return your fake colllection instead of doing a web request to fetch the same.

    However, JustMock is testing tool. So doing such is best recommended inside a test class. This is also the goal that will make testing a portion of your code easy without worrying about disk or webserver.

    Hope this information helps.

    Kind Regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top