1

I have a ViewModel like this:

public class BraintreeCallbackApiModel
{
    [ModelBinder(Name = "bt_signature")]
    public string Signature { get; set; }

    [ModelBinder(Name = "bt_payload")]
    public string Payload { get; set; }
}

The values are POSTed to controller as x-www-form-urlencoded parameters. I want to unit test the binding behavior - that bt_signature field is properly mapped to Signature.

I would like to avoid using TestServer and HttpClient as it's not needed overhead in this case.

I looked at ModelBinder tests on GitHub but couldn't get it to bind to my model class.

Michal Dymel
  • 4,312
  • 3
  • 23
  • 32

1 Answers1

0

As @SirRufo said, the unit test, in this case, may only check that ModelBinder class is working well, that has been already tested by ASP.NET Core team.

If you want to have a test, that will be broken when someone changes bt_signature field name (either in the model class or in request parameters), then you need to write a feature test. And so you need to run your app using TestServer and do a real request (via HttpClient as an example).

Look into What's the difference between unit, functional, acceptance, and integration tests? if interesting.

Set
  • 47,577
  • 22
  • 132
  • 150
  • 1
    Of course, I don't intend to test if `ModelBinder` works fine ;) I wan't to make sure, the binding config for this model is correct. As I said in the question, I know I can do it with `TestServer`, but wanted to make it lighter with a unit test... – Michal Dymel Jun 22 '17 at 06:38