【问题标题】:How do I write an MSpec test for user permissions in a special folder?如何为特殊文件夹中的用户权限编写 MSpec 测试?
【发布时间】:2014-08-14 13:20:46
【问题描述】:

我有一个便携式应用程序更新程序。在更新应用程序之前,我们检查登录用户是否具有对工作目录的写入权限。有谁知道如何编写断言这些特权的测试?

这是相关代码sn -p:

try
{
    var security = FolderAndFiles
       .WorkingDirectory
       .GetAccessControl()
       .GetAccessRules(true, true, typeof (NTAccount));
}
catch(UnauthorizedAccessException)
{
    // throw exception
}

【问题讨论】:

    标签: c# unit-testing user-permissions mspec


    【解决方案1】:

    我处理这个问题的一种方法是创建一个文件系统的抽象,它只包含我需要的工具。在此示例中,我创建了一个实用程序来从 Git 日志历史记录中提取信息。我将所有方法都设为虚拟,以便可以模拟它们,但您也可以轻松定义接口。

    /// <summary>
    ///   Class FileSystemService - an abstraction over file system services.
    ///   This class consists mainly of virtual methods and exists primarily to aid testability.
    /// </summary>
    public class FileSystemService
        {
        public virtual bool DirectoryExists(string path)
            {
            return Directory.Exists(path);
            }
    
        public virtual string PathCombine(string path1, string path2)
            {
            return Path.Combine(path1, path2);
            }
    
        public virtual string GetFullPath(string path)
            {
            return Path.GetFullPath(path);
            }
    
        public virtual void SaveImage(string path, Bitmap image, ImageFormat format)
            {
            image.Save(path, ImageFormat.Png);
            }
        }
    

    创建文件系统服务后,将其注入任何需要它的对象,如下所示:

    class SomeClassThatNeedsTheFileSystem
        {
        public SomeClassThatNeedsTheFileSystem(FileSystemService filesystem = null)
            {
            fileSystem = filesystem ?? new FileSystemService();
            }
        }
    

    注意:这是一个相当小的项目,我不想参与 IoC 容器,所以我做了“穷人的 IoC”,将 FileSystemService 设为默认值为“null”的可选参数;然后,我在构造函数中测试 null 并新建一个 FileSystemService。理想情况下,对于更健壮的代码,我会将参数设置为强制参数并强制调用者传入 FileSystemService。

    当需要创建一个假货时,我会这样做(我正在使用 MSpec 和 FakeItEasy):

    // Some stuff elided for clarity
    public class with_fake_filesystem_service
        {
        Establish context = () =>
            {
            Filesystem = A.Fake<FileSystemService>();
            };
    
        protected static FileSystemService Filesystem;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2021-11-05
      相关资源
      最近更新 更多