Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:48:44+00:00 2026-06-07T11:48:44+00:00

As far as I can tell, the answer is no. The issue I’m seeing

  • 0

As far as I can tell, the answer is no. The issue I’m seeing comes from the Include(params string[]) method in the System.Web.Optimization.Bundle class. Internally this invokes System.Web.Optimization.IncludeDirectory(string, string, bool), which in turn uses this code:

DirectoryInfo directoryInfo = new DirectoryInfo(
    HttpContext.Current.Server.MapPath(directoryVirtualPath));

While it is possible to set HttpContext.Current during a unit test, I can’t figure out how to make its .Server.MapPath(string directoryVirtualPath) return a non-null string. Since the DirectoryInfo(string) constructor throws an exception when passed a null argument, such a test will always fail.

What is the .NET team’s recommendation for this? Do we have to unit test bundling configurations as part of integration tests or user acceptance tests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:48:46+00:00Added an answer on June 7, 2026 at 11:48 am

    I have some good news for you, for RTM we added a new static property on BundleTable to enable more unit tests:

    public static Func<string, string> MapPathMethod;
    

    Edit Updated with a test virtual path provider:

    So you can do something like this:

    public class TestVirtualPathProvider : VirtualPathProvider {
    
        private string NormalizeVirtualPath(string virtualPath, bool isDirectory = false) {
            if (!virtualPath.StartsWith("~")) {
                virtualPath = "~" + virtualPath;
            }
            virtualPath = virtualPath.Replace('\\', '/');
            // Normalize directories to always have an ending "/"
            if (isDirectory && !virtualPath.EndsWith("/")) {
                return virtualPath + "/";
            }
            return virtualPath;
        }
    
        // Files on disk (virtualPath -> file)
        private Dictionary<string, VirtualFile> _fileMap = new Dictionary<string, VirtualFile>();
        private Dictionary<string, VirtualFile> FileMap {
            get { return _fileMap; }
        }
    
        public void AddFile(VirtualFile file) {
            FileMap[NormalizeVirtualPath(file.VirtualPath)] = file;
        }
    
        private Dictionary<string, VirtualDirectory> _directoryMap = new Dictionary<string, VirtualDirectory>();
        private Dictionary<string, VirtualDirectory> DirectoryMap {
            get { return _directoryMap; }
        }
    
        public void AddDirectory(VirtualDirectory dir) {
            DirectoryMap[NormalizeVirtualPath(dir.VirtualPath, isDirectory: true)] = dir;
        }
    
        public override bool FileExists(string virtualPath) {
            return FileMap.ContainsKey(NormalizeVirtualPath(virtualPath));
        }
    
        public override bool DirectoryExists(string virtualDir) {
            return DirectoryMap.ContainsKey(NormalizeVirtualPath(virtualDir, isDirectory: true));
        }
    
        public override VirtualFile GetFile(string virtualPath) {
            return FileMap[NormalizeVirtualPath(virtualPath)];
        }
    
        public override VirtualDirectory GetDirectory(string virtualDir) {
            return DirectoryMap[NormalizeVirtualPath(virtualDir, isDirectory: true)];
        }
    
        internal class TestVirtualFile : VirtualFile {
            public TestVirtualFile(string virtualPath, string contents)
                : base(virtualPath) {
                Contents = contents;
            }
    
            public string Contents { get; set; }
    
            public override Stream Open() {
                return new MemoryStream(UTF8Encoding.Default.GetBytes(Contents));
            }
        }
    
        internal class TestVirtualDirectory : VirtualDirectory {
            public TestVirtualDirectory(string virtualPath)
                : base(virtualPath) {
            }
    
            public List<VirtualFile> _directoryFiles = new List<VirtualFile>();
            public List<VirtualFile> DirectoryFiles {
                get {
                    return _directoryFiles;
                }
            }
    
            public List<VirtualDirectory> _subDirs = new List<VirtualDirectory>();
            public List<VirtualDirectory> SubDirectories {
                get {
                    return _subDirs;
                }
            }
    
            public override IEnumerable Files {
                get {
                    return DirectoryFiles;
                }
            }
    
            public override IEnumerable Children {
                get { throw new NotImplementedException(); }
            }
    
            public override IEnumerable Directories {
                get { 
                    return SubDirectories;
                }
            }
        }
    

    And then write a unit test using that like so:

        [TestMethod]
        public void StyleBundleCustomVPPIncludeVersionSelectsTest() {
            //Setup the vpp to contain the files/directories
            TestVirtualPathProvider vpp = new TestVirtualPathProvider();
            var directory = new TestVirtualPathProvider.TestVirtualDirectory("/dir/");
            directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/dir/style1.0.css", "correct"));
            directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/dir/style.css", "wrong"));
            vpp.AddDirectory(directory);
    
            // Setup the bundle
            ScriptBundle bundle = new ScriptBundle("~/bundles/test");
            bundle.Items.VirtualPathProvider = vpp;
            bundle.Include("~/dir/style{version}.css");
    
            // Verify the bundle repsonse
            BundleContext context = SetupContext(bundle, vpp);
            BundleResponse response = bundle.GetBundleResponse(context);
            Assert.AreEqual(@"correct", response.Content);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very strange because as far as I can tell the method does
That's basically it, as far as I can tell. I can include or require_once
my problem stems from the use of wxApp as far as I can tell.
Is the top-voted answer given here secure? As far as I can tell, the
As far as I can tell i'm doing everything by the book, but the
As far as I can tell, there is no API (official or unofficial) to
As far as I can tell, Scala has definitions for the Enumeration Value class
As far as I can tell, this code is fine, and should display some
As far as I can tell I wouldn't think it would make any difference
I have this trigger which works functionally (as far as I can tell by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.