T4MVC 和使用 MvcContrib.TestHelpers 进行测试存在静态链接内容问题
在开始处理之前,我试图围绕之前编写的一些代码编写一些测试。我遇到了控制器方法引用 T4MVC 为图像和链接创建的一些静态变量的问题。这实际上与我之前的问题是同一问题这里,但它不在构造函数中。
问题是这样的代码:
if (User.IsInRole("Rate Admin") || User.IsInRole("Administrator"))
{
_ratesViewData.ActionLinks = new List<CustomActionLink>
{
new CustomActionLink("Edit",
editPath + Resources.DelimeterHyphen,
Links.Content.Images.openwhite_gif),
new CustomActionLink("Delete",
statusPath + Resources.DelimeterHyphen,
Links.Content.Images.openwhite_gif)
};
}
问题是 Links.Content.Images.openwhite_gif,在 T4MVC 生成的代码中,它从静态方法 ProcessVirtualPath 调用 VirtualPathUtility.ToAbsolute。我似乎无法模拟 ProcessVirtualPath 或 VirtualPathUtility。
现在 ProcessVirtualPath 上面的评论说它是通过委托调用的,以允许替换它以进行单元测试。委托是:
public static Func<string, string> ProcessVirtualPath = ProcessVirtualPathDefault;
如何替换 ProcessVirtualPath 所调用的委托以允许单元测试。我不在乎它是否真的得到了一条有效的路径,我只是不想让它爆炸。我可以通过我的测试方法做到这一点吗?无需更改代码来测试它是否在非测试项目中进行调试?
另外一个相关的问题是像上面这样的一段代码的最佳实践是什么?哪里应该有基于权限的条件的代码?甚至是动作链接。我不确定为什么它们位于视图数据模型中。
好的,我确实让它与评论中提到的代码一起使用。
T4MVCHelpers.ProcessVirtualPath = (s) => "~/Content/Images";
但是仅当测试单独运行时,如果与使用 TestControllerBuilder 类且未设置它的另一个测试一起运行,任何需要此测试的测试都会失败。为什么?
I'm trying to write some tests around some code previously written before I start mucking with it. I'm running into issues where the controller method references some of the static variables that T4MVC makes for images and links. It is actually the same problem as my previous question here, but it's not in a constructor.
The issue is code like this:
if (User.IsInRole("Rate Admin") || User.IsInRole("Administrator"))
{
_ratesViewData.ActionLinks = new List<CustomActionLink>
{
new CustomActionLink("Edit",
editPath + Resources.DelimeterHyphen,
Links.Content.Images.openwhite_gif),
new CustomActionLink("Delete",
statusPath + Resources.DelimeterHyphen,
Links.Content.Images.openwhite_gif)
};
}
The problem is the Links.Content.Images.openwhite_gif, down in T4MVC generated code it calls VirtualPathUtility.ToAbsolute from the static method ProcessVirtualPath. I can't seem to mock ProcessVirtualPath or the VirtualPathUtility.
Now a comment above ProcessVirtualPath says it is called through a delegate to allow it to be replaced for unit testing. The delegate is:
public static Func<string, string> ProcessVirtualPath = ProcessVirtualPathDefault;
How do I replace that is being called for ProcessVirtualPath to allow for unit testing. I don't care if it really gets a valid path, I just don't want it to blow up. Can I do that from my test method? With out changing code to test if it's in debug in a non test project?
Also a related question is what is best practice for a piece of code like above? Where should one have code for permissions based conditions? Or even the actionlinks. I'm not sure why they are in a viewdata model.
OK I did get this to work with code mentioned in comment.
T4MVCHelpers.ProcessVirtualPath = (s) => "~/Content/Images";
BUT only when the test is ran individually, any test that needs this will fail if it is ran with another test that uses the TestControllerBuilder class and doesn't set it. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我没有完全理解这个问题,但为什么不能将 T4MVCHelpers.ProcessVirtualPath 设置为其他方法?
Maybe I'm not fully understanding the question, but why can't you just set T4MVCHelpers.ProcessVirtualPath to some other method?
如果我在测试类的静态构造函数中设置 ProcessVirtualPath 委托,我就能够使其正常工作。
I was able to get this to work if I set the ProcessVirtualPath delegate in a static constructor on my test class.