Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对ASP.NET捆绑包进行单元测试_Asp.net_Unit Testing_Asp.net Bundling - Fatal编程技术网

如何对ASP.NET捆绑包进行单元测试

如何对ASP.NET捆绑包进行单元测试,asp.net,unit-testing,asp.net-bundling,Asp.net,Unit Testing,Asp.net Bundling,我尝试了以下关于测试单元测试的内容,但是当我尝试枚举要测试的包中的文件时,我总是得到零计数 以下代码有什么问题 [TestFixture] public class BundleConfigTests { private TestVirtualPathProvider _vpp; private BundleCollection _bundles; private HttpContextBase _httpContext; [SetUp] public

我尝试了以下关于测试单元测试的内容,但是当我尝试枚举要测试的包中的文件时,我总是得到零计数

以下代码有什么问题

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

参考资料:

我也在使用这个例子,发现了这个问题。我通过在我的
bundle.Include()
中使用
{version}
并将
-1.0.css
附加到虚拟文件名来修复它。不知道它为什么会起作用,但这是我获得捆绑包中文件的唯一方法。希望有帮助

//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);
//设置vpp以包含文件/目录
var vpp=newtestVirtualPathProvider();
var directory=newtestvirtualpathProvider.TestVirtualDirectory(“/dir/”);
directory.DirectoryFiles.Add(新的TestVirtualPathProvider.TestVirtualFile(“/dir/select2-1.0.css”,“correct”));
AddDirectory(目录);
//设置捆绑包
var bundleCollection=新bundleCollection();
var bundle=newscriptbundle(“~/bundles/test”);
BundleTable.VirtualPathProvider=vpp;
bundle.Include(“~/dir/select2-{version}.css”);
bundleCollection.Add(bundle);
var mockHttpContext=new Mock();
//验证bundle repsonse
var context=new BundleContext(mockHttpContext.Object,bundleCollection,vpp.ToString());
var response=bundle.generateBundlerResponse(上下文);
Assert.Equal(@“correct”,response.Content);
//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);