Unit Testing - Validation of ViewModel ASP.NET MVC 2
- by dean nolan
I am currently unit testing a service that adds users to a repository.
I am using dependency injection to test using a fake repository.
The repository has a method CreateUser(User user) which just adds it to the database or in this case a List of Users.
The logic for the creation is in the UserServices class.
The application has a form for creating a user that requires some properties such as name and address.
This is an MVC 2 app and I will be using the new validation using data annotations.
This makes me wonder about a few things:
1) Should I annotate a POCO object that will map to the database? Or should I create a specific View Model that has these annotations and pass this data to the UserServices class?
2)Should the UserServicesClass also check this data? Would I best be constructing a Usr out of the ViewModel and passing this into the Service as a parameter?
3) The actual unit testing would depend on 2), I either populate a User object and pass that in, or I pass a large list of strings to the method CreateUser.
Writing this out I get a basic idea that I should probably annotate the view model only, pass in a user (constructed by the view model if the data is valid) and also just construct the user in the unit test also.
Is this the best way to go?