NullReferenceException when testing DefaultModelBinder.
Posted
by Byron Sommardahl
on Stack Overflow
See other posts from Stack Overflow
or by Byron Sommardahl
Published on 2010-04-25T00:04:29Z
Indexed on
2010/04/25
0:13 UTC
Read the original article
Hit count: 479
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 bind 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", "[email protected]"},
{ "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(_controller.ControllerContext, _bindingContext);
};
Any clue what it could be?
Feel free to ask me for more info, if needed. I might have taken something for granted.
© Stack Overflow or respective owner