- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是单元和集成测试的新手。我有一个类,其中包含一些用于删除指定目录中的多个文件并将文件移动到不同目录的方法。总共有3种方法。完整类代码:
public class FilesUtility : IFilesUtility
{
public void TidyUpXmlFiles(bool isDebug, string XmlFileDirectory)
{
if (isDebug)
{
MoveXmlFiles(XmlFileDirectory);
}
else
{
DeleteXmlFiles(XmlFileDirectory);
}
}
private static void MoveXmlFiles(string XmlFileDirectory)
{
var di = new DirectoryInfo(XmlFileDirectory);
// Create a subdirectory in the parent directory called XmlArchive.
DirectoryInfo destinationDirectory = di.CreateSubdirectory("XmlArchive");
string destinationDir = destinationDirectory.ToString();
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}
foreach (string file in Directory.GetFiles(XmlFileDirectory,
"*.Xml"))
{
// Use static Path method to extract only the file name from the path.
string fileName = Path.GetFileName(file);
if (fileName != null)
{
string destFile = Path.Combine(destinationDir, fileName);
//if the same file exists in the destination folder, delete the file and move the new file.
if (File.Exists(file))
{
File.Delete(destFile);
File.Move(file, destFile);
}
}
}
}
private static void DeleteXmlFiles(string XmlFileDirectory)
{
foreach (var file in Directory.GetFiles(XmlFileDirectory,
"*.Xml"))
{
File.Delete(file);
}
}
public void MoveFaultyXml(string XmlFileDirectory, string fileFullPath)
{
var di = new DirectoryInfo(XmlFileDirectory);
// Create a subdirectory in the parent directory called XmlArchive.
var destinationDirectory = di.CreateSubdirectory("FaultyXml");
var faultyXmlFileName = Path.GetFileName(fileFullPath);
var destinationDir = destinationDirectory.ToString();
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}
if (faultyXmlFileName != null)
{
string destFile = Path.Combine(destinationDir, faultyXmlFileName);
//if the same file exists in the destination folder, delete the file and move the new file.
if (!File.Exists(fileFullPath)) return;
File.Delete(destFile);
File.Move(fileFullPath, destFile);
}
}
我已经为这个方法创建了一个接口(interface)来使用 Moq 来模拟,如果这是我需要做的我不确定。接口(interface)代码为:
public interface IFilesUtility
{
void TidyUpXbrlFiles(bool isDebug, string xbrlFileDirectory);
void MoveFaultyXbrl(string xbrlFileDirectory, string fileFullPath);
}
现在我希望能够测试上述方法以确保它们实际上可以从目录中获取 XML 文件列表并删除/移动所有这些文件。我一直在阅读有关最小起订量和 mock 的内容。我也一直在阅读有关依赖注入(inject)的内容,但我的问题是我已经阅读了太多,现在想不出从哪里开始。
我希望有人可以使用上面发布的示例代码向我解释我如何为这段代码构建一个成功的测试。我已经在 Stackoverflow 上阅读了很多关于此的问题,我希望您不要将其作为重复项关闭,因为就像我说的那样,我的问题是我已经阅读了太多关于此的内容。如果有人可以使用我示例中的其中一种方法向我解释,我想我可能会理解它。
我不知道如何执行正确的 [TestFixtureSetup],我认为在我的情况下我需要它,我可以获得要在每个测试中使用的文件列表。
感谢所有花时间回答的人:)
最佳答案
这是我测试文件处理方法的方法:
这是测试代码...
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using System.IO;
public class FilesUtilityTests {
[Test]
public void TidyUpXmlFilesDeletesXmlFilesWhenNotDebugging() {
string testFilePath = TestFileHelper.UseTestMasterFile("YourXmlFile.xml");
FilesUtility util = new FilesUtility();
util.TidyUpXmlFiles(false, TestFileHelper.TestDirectoryName);
Assert.IsFalse(File.Exists(testFilePath));
// any other tests here.
}
}
// Obviously, this could be in a separate dll that you reference on all test projects.
public static class TestFileHelper {
public const string TestFolderName = @"Testing\UnitTests";
public const string DefaultMasterFilesFolderName = "MasterFiles";
public static string DefaultTestDirectoryName {
get { return Path.Combine(Path.GetDirectoryName(System.Environment.CurrentDirectory), TestFolderName); }
}
public static string TestDirectoryName {
get { return testDirectoryName ?? DefaultTestDirectoryName; }
set { testDirectoryName = value; }
}
private static string testDirectoryName;
public static string MasterFilesFolderName {
get { return masterFilesFolderName ?? DefaultMasterFilesFolderName; }
set { masterFilesFolderName = value; }
}
private static string masterFilesFolderName;
public static string TestFileExtension { get; set; }
public static string BuildTestFileName(string fileName) {
if (String.IsNullOrWhiteSpace(Path.GetPathRoot(fileName)))
fileName = Path.Combine(TestDirectoryName, fileName);
if (String.IsNullOrEmpty(Path.GetExtension(fileName)))
fileName = Path.ChangeExtension(fileName, TestFileExtension);
return fileName;
}
public static string BuildTestMasterFileName(string fileName) {
if (Path.IsPathRooted(fileName))
return Path.Combine(Path.Combine(Path.GetDirectoryName(fileName), MasterFilesFolderName), Path.GetFileName(fileName));
else
return Path.Combine(Path.Combine(TestDirectoryName, MasterFilesFolderName), fileName);
}
public static string UseTestMasterFile(string fileName) {
string dest = BuildTestFileName(fileName);
string source = BuildTestMasterFileName(dest);
DeleteTestFile(dest);
ClearReadOnlyAttributes(source);
File.Copy(source, dest, true);
return dest;
}
public static void DeleteTestFile(string filePath) {
if (String.IsNullOrWhiteSpace(filePath)) { return; }
if (!File.Exists(filePath)) { return; }
ClearReadOnlyAttributes(filePath);
File.Delete(filePath);
}
public static void ClearReadOnlyAttributes(string filePath) {
if (String.IsNullOrWhiteSpace(filePath)) { return; }
if (!File.Exists(filePath)) { return; }
FileAttributes attributes = File.GetAttributes(filePath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(filePath, attributes ^ FileAttributes.ReadOnly);
}
public static void SetReadOnlyAttributes(string filePath) {
if (String.IsNullOrWhiteSpace(filePath)) { return; }
if (!File.Exists(filePath)) { return; }
FileAttributes attributes = File.GetAttributes(filePath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly);
}
}
关于c# - 使用NUnit从指定目录删除多个文件的单元测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639249/
我正在为我的程序编写安装脚本,它应该在 Linux/Unix 操作系统上运行。以下文件的默认目录是什么: 可执行文件(程序)。程序应通过从命令行键入其名称来执行。 共享库。 第三方共享库(程序未开源,
我有一堆用户、组和应用程序注册,我的 MVC 应用程序使用 AAD 数据进行身份验证和授权。是否可以将 Azure Active Directory 从一个租户(目录)迁移到另一个租户(目录)?如果可
查看 cljsbuild 文档 https://github.com/emezeske/lein-cljsbuild :cljsbuild { :builds [{ ; The
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作。
我想使用\tableofcontents 命令,但没有目录从新页面开始或在末尾创建新页面,并且所有内容都是单倍行距。我怎样才能做到这一点?我假设使用 tocloft,但有哪些选择? 谢谢 最佳答案 试
我有一些 javascript 菜单代码,可以在单独的目录中正常工作。但是,当我尝试从同一目录中调用相同的 .js 文件时,它不会看到这些文件。 以下内容来自另一个目录: script type="t
我有这样的路径: /my/path/to/important_folder 在同一级别上,我还有其他文件和文件夹想要在达到与 important_folder 相同的级别时列出。 我的文件夹可能更深,
1、获取文件路径实现 1.1 获取当前文件路径 ? 1
我正在使用最新版本的 NTEmacs。 我写了一个名为“.dir-locals.el”的文件,如下所示。 ((nil . ((tab-width . 8) (fill-column .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
在我的 .vimrc 中有这些行 :set foldmethod=marker :set foldmarker=SECTION:,ENDSECTION: 用于自定义代码折叠。在我的文件中,相关语言的注
在 fish 中: for x in * echo $x end *这里包括所有目录和文件,如何只列出文件(或目录)? 最佳答案 fish 没有很多花哨的通配语法。但是,目录可以像这样迭代: f
这是我的目录结构: ├── src │ ├── helpers │ │ ├── __init__.py │ │ ├── foo.py │ │ └── bar.py │
我想递归重命名文件夹/目录名称并找到 this solution所以。但是这个命令没有效果 find . -type f -exec rename 's/old/new/' '{}' \; 这是一个正
我想在相册中创建一个文件夹,并希望将图像保存在创建的相册中。 这可能吗?有什么办法可以做到这一点吗? 我已经搜索过,大多数人都说这是不可能的。 感谢您的帮助。 最佳答案 您也许可以使用AssetsLi
如何在python中使用用户定义的名称创建临时文件/目录。我知道 tempfile .但是我看不到任何以文件名作为参数的函数。 注意:我需要这个来对包含临时文件的临时目录上的 glob(文件名模式匹配
我在项目中使用JaCoCo Gradle插件。 作为问题的一个示例,我的大部分代码都在com.me.mysoftware包下。 我正在使用代码生成器来生成build/generated/java/..
我正在尝试使用 Gradle 开始运行 jar 文件 我的任务如下所示: task startServer(type: Exec) { workingDir file("${buildDir}/a
如何在 Ant 中定义一个目录集,其中包括两个目录:项目的基目录和子目录“test”? 看起来您无法使用“/”、“.”或“”专门包含目录集的根目录。例如,这包括“./test”,但不包括“.”:
我正在使用 CTAGs 包,它使用 Sublime Text 2 生成两个文件 .tags 和 .tags_sorted_by_file。 那么当我进行项目搜索(CMD + SHIFT + F)时,如
我是一名优秀的程序员,十分优秀!