Moles VS2010 Windows 服务单元测试失败

发布于 2024-11-06 09:52:19 字数 3361 浏览 0 评论 0原文

我正在编写一个相对较小且简单的 Windows 服务,并使用 Moles 来模拟单元测试。由于代码较小,我决定使用 Moles 检测,而不是使用存根对代码进行分段。当我对模制程序集执行任何单元测试时,我收到错误:

初始化检测到丢失的监视器目录 失败:测试方法 FtpDirWatcher.Test.FileWatcherTest.InitilaizationDetectsMissingMonitorDirectory 抛出异常: Microsoft.Moles.Framework.Moles.MoleInvalidOperationException:

Moles 要求在仪表化过程中进行测试。

在 Visual Studio Test 中,将以下属性添加到您的单元测试方法:

[测试方法]

[HostType("Moles")] // 添加此属性

公共无效 测试(){...}

我不确定“Moles 要求测试仪表化过程中”是什么意思。请注意,“IN”意味着这不是通常的“Moles 要求测试是一个仪表化过程”。我回顾了文档,看看是否有任何我遗漏的内容。我显然还缺少一些重要的东西。

目标程序集(“FtpDirWatcher”)确实由 Moles 检测(通过 MFileWatcher 对象的存在证明),并且我在测试方法中具有适当的属性。我什至尝试将目标属性转换为方法,但无济于事。那么,这是怎么回事?

这是精简代码,所以没有批评!

using System;
using System.IO;
using System.Linq;
using FtpDirWatcher.Moles;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MolesAssemblySettings(Bitness = MolesBitness.AnyCPU)]

namespace .Test  // Test project namespace
{
    [TestClass]
    public class FileWatcherTest
    {
        readonly string _invalidDirectory = @"B:\invaliddirectory";

        [TestMethod]
        [DeploymentItem("FileWatcher.exe")]
        [HostType("Moles")]
        public void InitilaizationDetectsMissingMonitorDirectory()
        {
            Assert.IsFalse(Directory.Exists(_invalidDirectory));

            // THE FOLLOWING LINE OF CODE THROWS THE ERROR.
            // Use moles to detour the MonitorDirectory property's Get
            // method to a delegate.
            MFileWatcher.AllInstances.MonitorDirectoryGet = watcher => 
                new DirectoryInfo(_invalidDirectory);

            // Don't use the accessor -- no private fields are accessed.
            var target = new FileWatcher();
            Assert.IsFalse(target.IsConfigurationOk);
        }
    }
}

任何帮助表示赞赏!

更新:添加了以下构建输出。在上面的代码中包含位数设置,以表明它不应该成为问题。

------ 全部重建已开始:项目:通用,配置:调试 x86 ------

常见-> C:...\Common\bin\x86\Debug\Common.dll

------ 全部重建已开始:项目:FtpDirWatcher,配置:调试 x86 ------

FtpDirWatcher -> C:...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe

------ 全部重建已开始:项目:FtpDirWatcher.Test,配置:调试 x86 ------

Microsoft Moles v0.94.51023.0 - http://research.microsoft.com/moles - .NET v4.0.30319

版权所有 (c) Microsoft Corporation 2007-2010。保留所有权利。

00:00:00.00>摩尔

Moles:信息:元数据:忽略引用 C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll

Moles:信息:元数据:不兼容的程序集位数,仅使用反射

Moles : 信息 : 元数据 : 加载 C:\...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe (仅反射)

Moles:信息:编译:输出程序集名称:FtpDirWatcher.Moles

痣 : 信息 : 代码 : 找到 4 种类型

Moles:信息:代码:可见性:导出或组装(FtpDirWatcher.Moles)

00:00:00.37>代码生成

  Moles : info : code : 在 C:\...\FtpDirWatcher.Test\obj\x86\Debug\Moles\befw\mgcs 生成代码

  00:00:00.52>存根生成

    Moles : 信息 : 代码 : 生成 2 个存根类型

  00:00:00.89>摩尔一代

    摩尔:信息:代码:生成 2 种摩尔类型

00:00:01.45>编译

  摩尔:信息:编译:摩尔程序集:C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll

00:00:02.37>摩尔生成器 0 个错误,0 个警告

FtpDirWatcher.测试 -> C:...\FtpDirWatcher.Test\bin\x86\Debug\FtpDirWatcher.Test.dll ========== 全部重建:3 成功,0 失败,0 跳过 ==========

I am writing a relatively small and simple Windows Service, and using Moles to mock unit tests. Due to the small code, I decided to use Moles instrumentation, rather than segmenting the code with stubs. When I execute any unit test against the moled assembly, I receive an error:

InitilaizationDetectsMissingMonitorDirectory
has failed: Test method
FtpDirWatcher.Test.FileWatcherTest.InitilaizationDetectsMissingMonitorDirectory
threw exception:
Microsoft.Moles.Framework.Moles.MoleInvalidOperationException:

Moles requires tests to be IN an instrumented process.

In Visual Studio Test, add the following attribute your unit test method:

[TestMethod]

[HostType("Moles")] // add this attribute

public void
Test() { ... }

I'm not sure what it means that "Moles requires tests to be IN an instrumented process." Note the "IN" means this is not the usual "Moles requires tests to be an instrumented process." I have looked back through documentation, to see if there is anything I have missed. I am obviously still missing something important.

The target assembly ("FtpDirWatcher") is indeed instrumented by Moles (evidenced by the presence of the MFileWatcher object), and I have the proper attributes in place on the test method. I even tried converting the target property to a method, to no avail. So, what's going on?

This is condensed code, so no criticisms!

using System;
using System.IO;
using System.Linq;
using FtpDirWatcher.Moles;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MolesAssemblySettings(Bitness = MolesBitness.AnyCPU)]

namespace .Test  // Test project namespace
{
    [TestClass]
    public class FileWatcherTest
    {
        readonly string _invalidDirectory = @"B:\invaliddirectory";

        [TestMethod]
        [DeploymentItem("FileWatcher.exe")]
        [HostType("Moles")]
        public void InitilaizationDetectsMissingMonitorDirectory()
        {
            Assert.IsFalse(Directory.Exists(_invalidDirectory));

            // THE FOLLOWING LINE OF CODE THROWS THE ERROR.
            // Use moles to detour the MonitorDirectory property's Get
            // method to a delegate.
            MFileWatcher.AllInstances.MonitorDirectoryGet = watcher => 
                new DirectoryInfo(_invalidDirectory);

            // Don't use the accessor -- no private fields are accessed.
            var target = new FileWatcher();
            Assert.IsFalse(target.IsConfigurationOk);
        }
    }
}

Any help is appreciated!

UPDATE: Added the following build output. Included the bitness setting in the code, above, to show that it should not be an issue.

------ Rebuild All started: Project: Common, Configuration: Debug x86 ------

Common -> C:...\Common\bin\x86\Debug\Common.dll

------ Rebuild All started: Project: FtpDirWatcher, Configuration: Debug x86 ------

FtpDirWatcher -> C:...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe

------ Rebuild All started: Project: FtpDirWatcher.Test, Configuration: Debug x86 ------

Microsoft Moles v0.94.51023.0 - http://research.microsoft.com/moles - .NET v4.0.30319

Copyright (c) Microsoft Corporation 2007-2010. All rights reserved.

00:00:00.00> moles

Moles : info : metadata : ignoring reference C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll

Moles : info : metadata : incompatible assembly bitness, using reflection only

Moles : info : metadata : loading C:\...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe (reflection only)

Moles : info : compilation : output assembly name: FtpDirWatcher.Moles

Moles : info : code : found 4 types

Moles : info : code : visibility: exported or assembly(FtpDirWatcher.Moles)

00:00:00.37> code generation

  Moles : info : code : generating code at C:\...\FtpDirWatcher.Test\obj\x86\Debug\Moles\befw\m.g.cs

  00:00:00.52> stubs generation

    Moles : info : code : generated 2 stub types

  00:00:00.89> moles generation

    Moles : info : code : generated 2 mole types

00:00:01.45> compiling

  Moles : info : compilation : Moles assembly: C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll

00:00:02.37> moles generator 0 errors, 0 warnings

FtpDirWatcher.Test -> C:...\FtpDirWatcher.Test\bin\x86\Debug\FtpDirWatcher.Test.dll
========== Rebuild All: 3 succeeded, 0 failed, 0 skipped ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2024-11-13 09:52:19

这是一个完整的面部护理解决方案。我意识到我正在使用 DevExpress Visual Studio 工具< /em> 放置在 IDE 代码窗口中测试方法和类旁边的加载项字形(图标)。 Moles 安装程序更改了 Visual Studio 测试工具,以包含 Moles 主机适配器的参数和开关。然而,DevExpress 并没有被修改。

可能有两种解决方案:

  1. 简单地直接使用 Visual Studio 测试工具执行 Moles 测试
  2. 修改 Visual Studio 的 DevExpress 工具以正确使用 Moles 主机

详细的概述和示例代码位于我的博客 大括号:
http://thecurlybrace.blogspot.com/2011/05/moles-requires-tests-to-be-in.html

This is a total face-palm solution. I realized I was executing tests by using the DevExpress Tools for Visual Studio add-in glyphs (icons) that are placed in the IDE code window, next to the test methods and classes. The Moles installer alters the Visual Studio testing tools, to include parameters and switches for the Moles host adapter. However, DevExpress was not modified.

Two solutions are possible:

  1. Simply execute Moles tests by using the Visual Studio test tools directly
  2. Modify DevExpress Tools for Visual Studio to correctly use the Moles host

A detailed overview and sample code is on my blog, The Curly Brace:
http://thecurlybrace.blogspot.com/2011/05/moles-requires-tests-to-be-in.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文