我正在写一个相对较小且简单的Windows服务,并使用鼹鼠来模拟单元测试。由于代码很小,我决定使用Moles工具,而不是用代码段来分割代码。当我执行对moled装配任何单元测试,我收到一个错误:“痣需要测试是IN的仪器化进程”摩尔VS2010单元测试Windows服务失败
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() { ... }
我不知道这意味着什么是请注意,“IN”意味着这不是通常的“鼹鼠需要测试成为仪器化过程”。我回顾了文档,看看有没有我错过的东西。我显然仍然缺少重要的东西。
目标组件(“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);
}
}
}
任何帮助表示赞赏!
更新:添加了以下构建输出。在上面的代码中包含位设置,以表明它不应该成为问题。
------ 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 ==========
感谢您的链接,但我已经尝试修改注册表项。 :/(抱歉,我没有意识到你没有StackOverflow帐户!) – 2011-05-10 23:11:22