测试 DefaultModelBinder 时出现 NullReferenceException
我正在使用 BDD/TDD 技术开发一个项目,并且我正在尽力坚持到底。我刚刚遇到的一个问题是对 DefaultModelBinder 进行单元测试。我正在使用 mspec 来编写我的测试。
我有一个像这样的类,我想绑定到:
public class EmailMessageInput : IMessageInput
{
public object Recipient
{
get; set;
}
public string Body
{
get; set;
}
}
这是我构建规范上下文的方式。我正在构建一个假表单集合并将其填充到一个 bindingContext 对象中。
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();
};
}
最后,当我从一个规范内部执行 .BindModel() 时,我的规范抛出一个空引用异常:
Because of = () =>
{
_model = _modelBinder.BindModel(null, _bindingContext);
};
有什么线索吗?
如果需要,请随时向我询问更多信息。我可能认为某些事情是理所当然的。
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(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕我必须从一个问题开始:为什么要测试默认模型绑定器?
DefaultModelBinder
类是框架的一部分,因此您不负责测试。您应该假设它是一个工作黑匣子。不管怎样,看看你的代码,我认为你想要完成的是获得一个绑定模型来进一步测试;我走在正确的轨道上吗?如果是这样,我建议您研究James Broome's MSpec extensions for MVC MVC
源代码带有几个示例;但它允许您,例如,通过简单地执行以下操作来调用控制器上的操作方法,并对(键入的!)ViewModel(您从控制器操作传递给视图的模型)进行操作
希望这会有所帮助
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
Hope this helps