2010-10-28 2 views
17

Quindi ho copiato il file sample code dalla home page di Moq in modo abbastanza dettagliato e sto ottenendo un'eccezione di proxy del castello.Perché l'eccezione di inizializzazione del tipo 'Moq.Proxy.CastleProxyFactory' quando si utilizza NET40-NoCastle?

Ecco il mio codice (come una console app per un campione più facile)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Moq; 

namespace MoqTestConsole 
{ 
    public interface ILoveThisFramework 
    { 
     bool DownloadExists(string s); 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Mock<ILoveThisFramework> mock = new Mock<ILoveThisFramework>(); 

      // WOW! No record/replay weirdness?! :) 
      mock.Setup(framework => framework.DownloadExists("2.0.0.0")).Returns(true); 

      // Hand mock.Object as a collaborator and exercise it, 
      // like calling methods on it... 
      ILoveThisFramework lovable = mock.Object; 
      bool download = lovable.DownloadExists("2.0.0.0"); 

      // Verify that the given method was indeed called with the expected value 
      mock.Verify(framework => framework.DownloadExists("2.0.0.0")); 
     } 
    } 
} 

Tutto compila bene, ma quando chiama mock.Object viene generata l'eccezione seguente.

System.TypeInitializationException was unhandled 
    Message=The type initializer for 'Moq.Mock`1' threw an exception. 
    Source=Moq 
    TypeName=Moq.Mock`1 
    StackTrace: 
     at Moq.Mock`1.<InitializeInstance>b__0() in d:\Code\moq\src\Source\Mock.Generic.cs:line 138 
     at Moq.PexProtector.Invoke(Action action) in d:\Code\moq\src\Source\PexProtector.cs:line 56 
     at Moq.Mock`1.InitializeInstance() in d:\Code\moq\src\Source\Mock.Generic.cs:line 136 
     at Moq.Mock`1.OnGetObject() in d:\Code\moq\src\Source\Mock.Generic.cs:line 153 
     at Moq.Mock.GetObject() in d:\Code\moq\src\Source\Mock.cs:line 152 
     at Moq.Mock.get_Object() in d:\Code\moq\src\Source\Mock.cs:line 147 
     at Moq.Mock`1.get_Object() in d:\Code\moq\src\Source\Mock.Generic.cs:line 131 
     at MoqTestConsole.Program.Main(String[] args) in C:\Projects\Test\MoqTestConsole\MoqTestConsole\Program.cs:line 25 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.TypeInitializationException 
     Message=The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception. 
     Source=Moq 
     TypeName=Moq.Proxy.CastleProxyFactory 
     StackTrace: 
      at Moq.Proxy.CastleProxyFactory..ctor() 
      at Moq.Mock`1..cctor() in d:\Code\moq\src\Source\Mock.Generic.cs:line 54 
     InnerException: System.IO.FileNotFoundException 
      Message=Could not load file or assembly 'Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The system cannot find the file specified. 
      Source=Moq 
      FileName=Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc 
      FusionLog==== Pre-bind state information === 
LOG: User = jsi-dev-001\jmacintyre 
LOG: DisplayName = Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc 
(Fully-specified) 
LOG: Appbase = file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/ 
LOG: Initial PrivatePath = NULL 
Calling assembly : Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920. 
=== 
LOG: This bind starts in default load context. 
LOG: No application configuration file found. 
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. 
LOG: Post-policy reference: Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc 
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core.DLL. 
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core/Castle.Core.DLL. 
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core.EXE. 
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core/Castle.Core.EXE. 

      StackTrace: 
       at Moq.Proxy.CastleProxyFactory..cctor() 
      InnerException: 

Così sembra essere un componente Castello proxy che manca, ma mi sto riferimento al binario dalla directory NET40-NoCastle.

Ultima versione di MOQ (Moq.4.0.10827)

E io sono nuovo a Moq, così che io possa fare qualcosa di estremamente denso.

risposta

19

Il problema era che stavo usando i binari NET40-NoCastle, il che significa che stai usando Castle Windsor e hai già i binari di Castle Windsor nel tuo progetto. Ho avuto l'errore che questa directory era per progetti che non utilizzare Castle Windsor.

In ogni caso, linea di fondo, se si ha lo stesso problema, il utilizza invece i binari delle directory NET40.

Grazie Ben Alabaster per la risposta via Twitter.

+2

Ho fatto lo stesso errore, non è la migliore denominazione che ho visto – Karsten

+3

Ho fatto esattamente esattamente la stessa cosa. I nomi delle directory non funzionano. Ho paura. –

+1

Cosa hanno detto! :-) –