How to unit test ASP.NET bundles
Asked Answered
D

2

3

I tried following this answer about testing unit tests but when I try to enumerate the files in the bundle to test, I always get count zero.

What's the problem with the following code?

[TestFixture]
public class BundleConfigTests
{
    private TestVirtualPathProvider _vpp;
    private BundleCollection _bundles;
    private HttpContextBase _httpContext;

    [SetUp]
    public void SetUp()
    {
        _vpp = new TestVirtualPathProvider();
        var directory = new TestVirtualPathProvider.TestVirtualDirectory("/css/");
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/global.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/global2.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/ie8.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/buttons.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/buttons2.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/navigation.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/dealticket.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/Content/themes/base/jquery-ui.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/Content/Site.css", "correct"));
        directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/jquery.loadmask.css", "correct"));
        _vpp.AddDirectory(directory);

        BundleTable.VirtualPathProvider = _vpp;

        _bundles = BundleTable.Bundles;
        _httpContext = MockRepository.GenerateMock<HttpContextBase>();
    }

    [Test]
    public void RegisterBundles_Always_AllCssFilesToEachBundle()
    {
        BundleConfig.RegisterBundles(_bundles);

        foreach (var bundle in _bundles.GetRegisteredBundles())
        {
            var bundledFiles = bundle.EnumerateFiles(new BundleContext(_httpContext, _bundles, bundle.Path));

            // bundledFiles has zero count!
            // Also tried getting the non-public Items property of the bundle which is also zero.
            // var items = bundle.GetType().GetProperty("Items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(bundle, null);

            foreach (var file in _vpp.GetDirectory("~/css/").Files.OfType<BundleConfigTests.TestVirtualPathProvider.TestVirtualFile>())
            {
                Assert.True(bundledFiles.Any(bf => bf.IncludedVirtualPath == file.VirtualPath), file.VirtualPath + " not bundled");
            }
        }
    }

    // Used this test this TestVirtualPathProvider by @HaoKung (https://mcmap.net/q/831605/-is-it-possible-to-unit-test-bundleconfig-in-mvc4)
}

My RegisterBundles implementation is:

public static void RegisterBundles(BundleCollection bundles)
{
  bundles.Add(new Bundle("~/bundles/styles-" + Cultures.English)
          .Include("~/css/global.css")); // This item is never in the items collection of the bundle in my unit tests.
  bundles.Add(new Bundle("~/bundles/styles-" + Cultures.Chinese));
}

References: Bundle unit test suggestion by Hao Kung

Dwight answered 19/9, 2014 at 9:25 Comment(0)
W
0

I was using that example too and found this issue. I fixed it by using a {version} in my bundle.Include() and appending -1.0.css to the virtual file name. No idea why it works, but it was the only way I could get the file in the bundle. Hope that helps.

//Setup the vpp to contain the files/directories
var vpp = new TestVirtualPathProvider();
var directory = new TestVirtualPathProvider.TestVirtualDirectory("/dir/");
directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/dir/select2-1.0.css", "correct"));
vpp.AddDirectory(directory);

// Setup the bundle
var bundleCollection = new BundleCollection();
var bundle = new ScriptBundle("~/bundles/test");
BundleTable.VirtualPathProvider = vpp;
bundle.Include("~/dir/select2-{version}.css");
bundleCollection.Add(bundle);
var mockHttpContext = new Mock<HttpContextBase>();

// Verify the bundle repsonse
var context = new BundleContext(mockHttpContext.Object, bundleCollection, vpp.ToString());
var response = bundle.GenerateBundleResponse(context);
Assert.Equal(@"correct", response.Content);
Whimwham answered 23/11, 2015 at 3:17 Comment(0)
R
0

I had the similar issue and it seems to come from the implementation of the TestVirtualPathProvider. When you run this :

directory.DirectoryFiles.Add(new TestVirtualPathProvider.TestVirtualFile("/css/global.css", "correct"));

It doesn't actually add the file to the FileMap (_fileMap) which means that it won't be found by the Bundle.

You'd need to edit the method AddDirectory in TestVirtualPathProvider from Hao Kung to be like this instead:


    public void AddDirectory(VirtualDirectory dir)
    {
        DirectoryMap[NormalizeVirtualPath(dir.VirtualPath, isDirectory: true)] = dir;

        foreach (var file in dir.Files)
        {
            AddFile((VirtualFile)file);
        }
    }

Which will actually add the files to the FileMap.

From what I understand and some bebugging, the only reason why adding {Version} works is because the file won't be found and System.Web.Optimization will have some sort of fallback that only works when the version is present.

Ragi answered 16/4, 2023 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.