测试 MVC 控制器操作 HttpAcceptAttribute 动词
对控制器操作 HttpAcceptAttribute 动词进行单元测试的最佳方法是什么?
到目前为止,我有以下内容,但它太丑了,连母亲都不喜欢它,而且不太灵活。有更好的办法吗?
[Fact] // using xUnit, mocking controller in class
public void FilterControllerTestRemoveFilterByProductAttributeIsOfTypePost()
{
Type[] paramTypes = new[] { typeof(int) };
var method = typeof(FilterController).GetMethod("MyMethod", paramTypes);
var attributes = method.GetCustomAttributes(typeof(AcceptVerbsAttribute), false).Cast<AcceptVerbsAttribute>().SingleOrDefault();
Assert.NotNull(attributes);
Assert.Equal(1, attributes.Verbs.Count());
Assert.True(attributes.Verbs.First().Equals(HttpVerbs.Post.ToString(), StringComparison.InvariantCultureIgnoreCase));
}
谢谢 苹果
What is the best way to unit test the controller action HttpAcceptAttribute verbs?
So far I have the following but it's so ugly even a mother couldn't love it and not very flexible. Is there a better way?
[Fact] // using xUnit, mocking controller in class
public void FilterControllerTestRemoveFilterByProductAttributeIsOfTypePost()
{
Type[] paramTypes = new[] { typeof(int) };
var method = typeof(FilterController).GetMethod("MyMethod", paramTypes);
var attributes = method.GetCustomAttributes(typeof(AcceptVerbsAttribute), false).Cast<AcceptVerbsAttribute>().SingleOrDefault();
Assert.NotNull(attributes);
Assert.Equal(1, attributes.Verbs.Count());
Assert.True(attributes.Verbs.First().Equals(HttpVerbs.Post.ToString(), StringComparison.InvariantCultureIgnoreCase));
}
Thanks
Mac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有反射和魔术字符串,可以轻松重命名控制器和方法而不破坏单元测试:
No reflection and magic strings, easy to rename controller and method without breaking the unit test:
使用
Using
达林解决方案的一点修改版本。
A bit modified version Darin`s solution.