我尝试按照有关测试单元测试的答案进行操作,但是当我尝试枚举捆绑中的文件进行测试时,我总是得到零计数。
以下代码有什么问题?
[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)
}
我的 RegisterBundles 实现是:
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));
}