开发者

NullReferenceException when testing DefaultModelBinder

开发者 https://www.devze.com 2022-12-27 21:19 出处:网络
I\'m developing a project using BDD/TDD techniques and I\'m trying my best to stay the course. A problem I just ran into is unit testing the DefaultModelBinder. I\'m using mspec to write my tests.

I'm developing a project using BDD/TDD techniques and I'm trying my best to stay the course. A problem I just ran into is unit testing the DefaultModelBinder. I'm using mspec to write my tests.

I have a class like this that I want to bi开发者_开发技巧nd to:

public class EmailMessageInput : IMessageInput
    {
        public object Recipient
        {
            get; set;
        }

        public string Body
        {
            get; set;
        }

    }

Here's how I'm building my spec context. I'm building a fake form collection and stuffing it into a bindingContext object.

public abstract class given_a_controller_with_valid_email_input : 
            given_a_controller_context
        {
            Establish additional_context = () =>
                               {
                                   var form = new FormCollection
                                                  {
                                                      new NameValueCollection
                                                          {
                                                              { "EmailMessageInput.Recipient", "test@test.com"},
                                                              { "EmailMessageInput.Body", "Test body." }
                                                          }
                                                  };

                                   _bindingContext = new ModelBindingContext
                                                         {
                                                             ModelName = "EmailMessageInput",
                                                             ValueProvider = form
                                                         };

                                   _modelBinder = new DefaultModelBinder();
                               };

            protected static ModelBindingContext _bindingContext;
            protected static DefaultModelBinder _modelBinder;
        }

        public abstract class given_a_controller_context
        {
            protected static MessageController _controller;

            Establish context =
                () =>
                    {
                        _controller = new MessageController();
                    };
        }

Finally, my spec throws an null reference exception when I execute .BindModel() from inside one of my specs:

Because of = () => 
                 {
                     _model = _modelBinder.BindModel(null, _bindingContext);
                 };

Any clue what it could be?

Feel free to ask me for more info, if needed. I might have taken something for granted.


I'm afraid I'll have to start with a question: why are you testing the default model binder? The DefaultModelBinder class is part of the framework and so it is not your responsibility to test. You should assume it is a working black box.

Anyway, looking at your code I think that what you're trying to accomplish is to get a bound model to test further; am I on the right track? if so, I would recommend you to look into James Broome's MSpec extensions for MVC

The source code comes with several examples; but it allows you, for example, to call an action method on a controller and act on the (typed!) ViewModel (the one you pass to the view from the controller action) by simply doing

It should_display_the_person_name = () => result.Model<Person>().Name.ShouldNotBeNull();

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消