2

비교적 작고 간단한 Windows 서비스를 작성하고 Moles를 사용하여 유닛 테스트를 모의합니다. 코드가 작기 때문에 코드를 스텁으로 분할하지 않고 Moles 계측을 사용하기로 결정했습니다. 나는 moled 조립에 대한 모든 단위 테스트를 실행하면, 나는 오류가 나타납니다. "두더지가 계측 된 과정에 로 테스트를해야합니다"나는 것을 무엇을 의미하는지 모르겠어요Moles 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"은 일반적으로 "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); 
     } 
    } 
} 

도움을 주시면 감사하겠습니다.

업데이트 : 다음 빌드 출력이 추가되었습니다. 위의 코드에서 bitness 설정을 포함시켜 문제가되지 않아야 함을 나타냅니다.

------ 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

이것은 전체적으로 손바닥 모양의 솔루션입니다. IDE 코드 창에있는 테스트 방법 및 클래스 옆에있는 DevExpress Tools for Visual Studio 추가 기능 글리프 (아이콘)를 사용하여 테스트를 실행하고 있다는 것을 깨달았습니다. Moles 설치 프로그램은 Moles 호스트 어댑터에 대한 매개 변수와 스위치를 포함하도록 Visual Studio 테스트 도구를 변경합니다. 그러나 DevExpress는 수정되지 않았습니다.

두 솔루션이 가능합니다 :

  1. 단순히 비주얼 스튜디오 테스트 도구를 직접
  2. 를 사용하여 두더지 테스트를 실행 제대로 두더지 호스트

상세한를 사용하는 비주얼 스튜디오에 대한

  • 수정 DevExpress의 도구 개요 및 샘플 코드는 내 블로그에 있습니다. The Curly Brace: http://thecurlybrace.blogspot.com/2011/05/moles-requires-tests-to-be-in.html