views:

98

answers:

1

Hi All,

I want to Test my code! A noble goal I'm sure you agree.

I need to mock out DataContexts for my Regression tests, running the test will verify that for any code change, given the same input we produce the same output.

I'm using Moles to mock out my data contexts. It's awesome and simple, I recommend it, I might move to Moq if it's more lightweight but that's another story.

To get data for my Mocked Data Contexts I want to serialize a small amount of live data stored in an MSSQL DB. To allow the Linq-To-SQL classes to be serialized I use a DataContractSerializer having set the Serialization Mode property to Unidirectional as instructed by MSFT. This allows me to serialize a row to XML.

When I want to serialize a table I must first pull it into memory by calling ToList(). This is because Linq.Data.Table is not itself serializable.

So now I have a file for each table, I can then Deserialize the XML files and create Linq Tables from the Lists. Then we create a delegate and use that as the implementation for the Mock Data Context. All of this is clearly explained in this screencast.

The problem I have is with maintaining relationships between tables, how can I serialize a row with all of its dependencies and then deserialize it in such a way that they're still accessible.

EXAMPLE

  • In my Live DB I have Tables Customers and Purchases
  • There exists a one to many relationship from Customers to Purchases
  • In the application code I wish to test I use syntax like;

    var firstCustomerPurchases = Customers.First().Purchases

  • This won't work in my test cases with my MockDataContext as the Customers and Purchases tables are just lists which aren't bound to each other.

How can I maintain the relationship throughout the serialization/deserialization process?

Thanks,

Gav

A: 

Have you tought about converting your Linq tables into a DataSet, which is serializable and maintains the relationships?

Peli