1

I have inherited code that tests an API that serves data. The customer supplies expected results as SQL code. The test code has a utility class with various overloads for VerifyResult that compares and ADO.NET Object with an object from the API. It doesn't seem very OOP so I'm looking at refactoring.

Here's some pseudo code:

[Test]
public void Book()
{
    DataRow expectedBook = 
        myTestUtility.GetBookUsingSqlForIsbn("978-0304359387");
    Book actualBook = API.GetBookForIsbn("978-0304359387");
    Assert.IsTrue(myTestUtility.VerifyResult(expectedBook, actualBook);
}

In test utility class:

protected bool VerifyResult(DataRow expectedBook, Book actualBook)
{
    Assert.AreEqual(expectedBook["ISBN"],  
                    actualBook.ISBN, 
                    "Book ISBN does not match");

    Assert.AreEqual(expectedBook["AuthorName"], 
                    actualBook.AuthorName, 
                    "Book AuthorName does not match");
    …
}

My first though at refactoring was to fabricate an API object (eg Book) using the values in the API object, then write a custom equality assertion. However, I didn't get far: because the API just serves data, its classes' properties are read only (internal set) so I can't create a Book object with custom values (API is all concrete classes using inheritance and no interfaces). Is there a way around this restriction?

If you can suggest an entirely different approach, please feel free!


Say the API classes look like this:

public class Point
{
    public int x { get; internal set; }
    public int y { get; internal set; }
}

public class Circle
{
    public int Radius { get; internal set; }
    public Point Pos { get; internal set; }
}

Is it possible to inherit these API types and make the properties writeable while allowing an instance of the API to be cast as the new subtype?

petemoloy
  • 695
  • 1
  • 5
  • 9
  • From the Utility class, you are violating basic Unit testing guideline. I mean, VerifyResult does too assertion, its always confusing which assertion failed and why. So split it out into 2 different unit tests. That way its always clear and concise. – Zenwalker Aug 23 '12 at 09:22
  • Two ways you can access those closed classes properties/methods. one via Reflection other is to make those closed properties from Book or API class as protected. Then add your own class and inherit from Book or API. Then you can expose some methods or some thing and call the base class properties which were closed earlier. – Zenwalker Aug 23 '12 at 09:25
  • @zenwalker: thanks, I don't think reflection will work (some properties are complex types), otherwise I'm trying to do as you advised and wondering how I can inherit from the API class and make the property writeable. – petemoloy Aug 23 '12 at 14:07
  • Assuming that your API class has a property SomeProperty{get;} Now you can alter it and add protected Set to it. Then you can inherit this class with your own class for testing purpose and then have a public method or property from which you can set the value to base class property. Hope this simple small example helps you http://pastebin.com/YgPsyFYv – Zenwalker Aug 24 '12 at 08:40
  • @zenwalker: ah, I see. Problem is, I can't change the API. Seems that doing so would violate a testing principle, [Don't Modify the System Under Test](http://xunitpatterns.com/Principles%20of%20Test%20Automation.html): "because we may no longer actually be testing the code we plan to put into production." – petemoloy Aug 24 '12 at 09:38
  • You can add some new methods/properties just for the maintainence activity (unit testing) and make your test assembly a friend assembly for the production application code base. That way you not comporimising any security in the production code. Regarding changing the API, you really need not to. Just extend. Perhaps you may like to read Art of Unit testing by Roy book. He has given good examples. – Zenwalker Aug 28 '12 at 03:59
  • @zenwalker: thanks for your advice, I think I may need a different approach. Will check out that book (I see there are a couple of free chapters on his website). – petemoloy Aug 30 '12 at 15:07

0 Answers0