David Hawkins
David Hawkins

Reputation: 1067

MVC Viewmodel TDD approach

When creating ASP.Net MVC Viewmodels what is the recommended approach using TDD?

I know what the viewmodel needs to consist of and how to test the validation attributes of the model, but I'm unsure what process to follow instead of diving in and just creating the viewmodel without tests.

One thought is to instantiate the controller and call the action, then assert that the returned model first exists and then that it has particular properties, however I think this will require reflection and I can't see that being a good approach.

Any advice would be much appreciated.

Kind regards,

David Hawkins

Upvotes: 0

Views: 181

Answers (1)

Spock
Spock

Reputation: 6992

The best way to look at this is from the bevaior that you need to test. With TDD you drive your behavior of your controller/Action and verify its accordingly. During this process you create view models as required. Your view model should be very simple and they do not require any TDD' tests.

Also try avoiding TDD with testing the scemantics if a view model such as whether properties exist using reflection. It does not add much value. It is important you TDD it is also important you would TDD the code that needed and had some behavior from the requirements point of view.

Some example of a typical ASP.NET MVC Unit Test with TDD you would verify a view model returns as the expected type, or whether a call to a repo has successfully executed, or whether the correct view name returns, view model contain any default values etc etc.

Upvotes: 1

Related Questions