如何在asp.net mvc中模拟AuthorizeAttribute?
我有自己的自定义授权属性,我正在尝试检查我的控制器方法以查看它们是否具有正确的角色。现在我的自定义授权标签中有数据库代码。
我模拟它的方式似乎不起作用,因为我发现的反射东西似乎只是不传递任何参数,因此授权属性中的默认构造函数会创建一个新的服务层对象,该对象创建一个存储库对象(这会杀死单元测试)。
var indexAction = typeof(Controller).GetMethod(method);
var authorizeAttributes = indexAction.GetCustomAttributes(typeof(AuthorizeAttribute), true);
//Assert
Assert.That(authorizeAttributes.Length > 0, Is.True);
foreach (AuthorizeAttribute att in authorizeAttributes)
{
Assert.That(att.Roles, Is.EqualTo(roles));
}
我的 AutorizeAttribute 的构造函数
public MyAuthorize()
{
authorize = new ServiceLayer();
}
public MyAuthorize(IServicelayer layer)
{
authorize = layer;
}
反射内容不断调用我的默认构造函数。我如何传入模拟服务层或其他东西?
谢谢
I have my own custom Authorize Attribute and I am trying to check my controller methods to see if they have the correct roles in place. Now my custom authorize tag has database code in it.
The ways I am mocking it up don't seem to work since the reflection stuff I found seems to to just pass no arguments so my default constructor in the Authorize Attribute gets hit creating a new service layer object that creates a repository object(that kills the unit test).
var indexAction = typeof(Controller).GetMethod(method);
var authorizeAttributes = indexAction.GetCustomAttributes(typeof(AuthorizeAttribute), true);
//Assert
Assert.That(authorizeAttributes.Length > 0, Is.True);
foreach (AuthorizeAttribute att in authorizeAttributes)
{
Assert.That(att.Roles, Is.EqualTo(roles));
}
Constructors of my AutorizeAttribute
public MyAuthorize()
{
authorize = new ServiceLayer();
}
public MyAuthorize(IServicelayer layer)
{
authorize = layer;
}
the reflection stuff keeps calling my default constructor. How can I pass in a mock service layer or something?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看过一些模拟框架吗?我过去曾用它们来伪造 http 上下文等。
这是另一篇 Stack Overflow 帖子,可能可以帮助您...
https://stackoverflow .com/questions/37359/what-c-mocking-framework-to-use
Have you looked at some of the Mocking Frameworks? I've used these to fake the http context etc in the past.
Here's another Stack Overflow post that might be able to help you...
https://stackoverflow.com/questions/37359/what-c-mocking-framework-to-use
我认为问题不在于您的代码,而在于您正在尝试测试的内容。什么决定了属性所具有的角色?
如果您根据传递到属性中的内容从服务层检索角色,您的测试应确认该属性存在于它所保护的操作上(控制器测试的一部分),并从以下位置对您的服务层进行适当的调用:属性(属性测试的一部分),并且服务层为特定请求返回适当的值(控制器测试的一部分)。
为了确保所有部分协同工作,您将需要使用基本上模仿整个请求管道的集成测试 - 像 Steve Sanderson 的 MvcIntegrationTest 这样的东西应该简化这个 http://blog.codeville.net/2009/06/11/integration-testing-your-aspnet-mvc-application /
I don't think the problem is with your code but what you are trying to test. What determines the roles that the attribute has?
If you are retrieving the roles from your service layer based on something passed into the attribute, your tests should confirm that the attribute exists on the action it is protecting ( part of the controller tests ), the appropriate calls are made to your service layer from the attribute ( part of the attribute tests ), and that the service layer returns the appropriate values for a specific request ( part of the controller tests ).
To ensure all of the parts work together, you will need to use integration tests that essentially mimic the entire request pipeline - something like Steve Sanderson's MvcIntegrationTest should simplify this http://blog.codeville.net/2009/06/11/integration-testing-your-aspnet-mvc-application/