How to integrate LinqToSql with metadata annotations
I'm just getting started on a new MVC project, and like a good boy, I'm trying to delay the move to DB as long as possible. Here's the scoop:
- I am planning on using ComponentModel.DataAnnotations decorations.
- I am also planning to use LinqToSql
Can I write a unit test against the DataAnnotations metadata classes? I don't want to host such a schema for as long as possible, but I would still like to write tests to validate the model.
Any ideas on a good approach? Maybe something completely different?
+1
a source to share
1 answer
Brad Wilson wrote a great blog post about using DataAnnotations and unit testing; http://bradwilson.typepad.com/blog/2009/04/index.html
The essence of the testing strategy is as follows:
- Write a test to validate to ensure that you are using the DataAnnotationsModelBinder as the default binder.
- Use reflection to ensure that the DataAnnotation attributes you have decorated with model properties are what you expect.
- When testing the Edit or Create Controller actions, artificially inject ModelErrors to verify that you handle them as expected.
The rationale is that you consider System.ComponentModel.DataAnnotations as part of the framework and just test how you used it.
+1
a source to share