如何单元测试ASP.NET束 [英] How to unit test ASP.NET bundles

查看:234
本文介绍了如何单元测试ASP.NET束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着以下<一个href=\"http://stackoverflow.com/questions/11371710/is-it-possible-to-unit-test-bundleconfig-in-mvc4/11441016#11441016\">this回答有关测试单元测试,但是当我试图枚举捆绑测试文件,我总是数为零。

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.

什么是与以下code中的问题?

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 (http://stackoverflow.com/a/11441016/169034)
}

我RegisterBundles实现是:

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));
}

参考:捆绑单元测试建议由郝骷嗯

推荐答案

我用这个例子也发现了这个问题。我用固定它 {版本} 在我的 bundle.Include()和追加 -1.0.css 来虚拟文件名称。不知道为什么它的工作原理,但它是我能得到包中的文件的唯一途径。希望有所帮助。

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);

这篇关于如何单元测试ASP.NET束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆