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?