2

我正在寫一個相對較小且簡單的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 ==========

回答

1

這是一個完整的臉部掌上解決方案。我意識到我正在使用DevExpress Tools for Visual Studio附加字形(圖標)執行測試,放置在IDE代碼窗口中,旁邊是測試方法和類。 Moles安裝程序會更改Visual Studio測試工具,以包含Moles主機適配器的參數和開關。但是,DevExpress沒有被修改。

兩種解決方案:

  1. 通過使用Visual Studio測試工具直接
  2. 簡單的執行痣測試
  3. 修改的DevExpress工具Visual Studio來正確使用痣主機

詳細概述和示例代碼在我的博客上,The Curly Brace: http://thecurlybrace.blogspot.com/2011/05/moles-requires-tests-to-be-in.html