سؤال

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?

هل كانت مفيدة؟

المحلول

Maybe I'm not fully understanding the question, but why can't you just set T4MVCHelpers.ProcessVirtualPath to some other method?

نصائح أخرى

I was able to get this to work if I set the ProcessVirtualPath delegate in a static constructor on my test class.

 public class BaseTest
 {
    static BaseTest()
    {
        T4MVCHelpers.ProcessVirtualPath = s => s.TrimStart('~');
    }

    // TEST CODE

 }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top