gpt4 book ai didi

unit-testing - 是否可以在MVC4中对BundleConfig进行单元测试?

转载 作者:行者123 更新时间:2023-12-04 03:22:32 25 4
gpt4 key购买 nike

据我所知,答案是否定的。我看到的问题来自Include(params string[])类中的System.Web.Optimization.Bundle方法。在内部,这会调用System.Web.Optimization.IncludeDirectory(string, string, bool),后者依次使用以下代码:

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

虽然可以在单元测试期间设置 HttpContext.Current,但我不知道如何使其 .Server.MapPath(string directoryVirtualPath)返回非空字符串。由于 DirectoryInfo(string)构造函数在传递null参数时会引发异常,因此此类测试将始终失败。

.NET团队对此有何建议?作为集成测试或用户验收测试的一部分,我们是否必须对捆绑配置进行单元测试?

最佳答案

对于您,我有个好消息,对于RTM,我们在BundleTable上添加了一个新的静态属性以启用更多的单元测试:

public static Func<string, string> MapPathMethod;

编辑已使用测试虚拟路径提供程序进行了更新:

因此,您可以执行以下操作:
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;
}
}
}

然后像这样编写一个单元测试:
    [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);
}

关于unit-testing - 是否可以在MVC4中对BundleConfig进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com