Moles VS2010 Windows 服务单元测试失败
我正在编写一个相对较小且简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
http://social.msdn.microsoft.com/Forums/en/pex/thread/176b2fc5-882e-413b-b4d5-10ea6b486e65
您使用的是 x64 吗?
Try this:
http://social.msdn.microsoft.com/Forums/en/pex/thread/176b2fc5-882e-413b-b4d5-10ea6b486e65
Are you using x64?
这是一个完整的面部护理解决方案。我意识到我正在使用 DevExpress Visual Studio 工具< /em> 放置在 IDE 代码窗口中测试方法和类旁边的加载项字形(图标)。 Moles 安装程序更改了 Visual Studio 测试工具,以包含 Moles 主机适配器的参数和开关。然而,DevExpress 并没有被修改。
可能有两种解决方案:
详细的概述和示例代码位于我的博客 大括号:
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:
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