我使用 Moq 作为单元测试的模拟库。
似乎很难找到温莎城堡库,因为它给了我这个错误:
System.TypeInitializationException : Une exception a été levée par l'initialiseur de type pour 'Moq.Mock`1'.
----> System.TypeInitializationException : Une exception a été levée par l'initialiseur de type pour 'Moq.Proxy.CastleProxyFactory'.
----> System.IO.FileNotFoundException : Impossible de charger le fichier ou l'assembly 'Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' ou une de ses dépendances. Le fichier spécifié est introuvable.
à Moq.Mock`1.<InitializeInstance>b__24_0()
à Moq.PexProtector.Invoke(Action action)
à Moq.Mock`1.InitializeInstance()
à Moq.Mock`1.OnGetObject()
à Moq.Mock.GetObject()
à Moq.Mock.get_Object()
à Moq.Mock`1.get_Object()
à TecLib.Common.Tests.Infrastructure.CqrsRepositoryBaseTest.SetUp() dans C:\Users\Ludovic\Documents\Visual Studio 2015\Projects\TEC2\TecLib.Common.Tests\Infrastructure\CqrsRepositoryBaseTest.cs:ligne 38
--TypeInitializationException
à Moq.Proxy.CastleProxyFactory..ctor()
à Moq.Mock`1..cctor()
--FileNotFoundException
à Moq.Proxy.CastleProxyFactory..cctor()
这是触发错误的测试之一。该代码工作得很好。触发错误的是 Moq 图书馆,因为由于某种原因它似乎无法找到温莎城堡图书馆。
[SetUp]
public void SetUp()
{
_eventStore = new Mock<IEventStore>();
_eventDispatcher = new Mock<IEventDispatcher>();
_entityFactory = new Mock<IEntityFactory>();
_repository = new CqrsAddressRepository(_entityFactory.Object, _eventStore.Object, _eventDispatcher.Object);
_addressInfo1 = new AddressInfo("400 chemin Jette", "", "", "Farnham", "J0J2B0", "Quebec", "Canada");
_addressInfo2 = new AddressInfo("322 rue Principale E", "", "", "Farnham", "J2N1L7", "Quebec", "Canada");
}
这些是我为该项目提供的包
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.3" targetFramework="net461" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net461" />
<package id="Moq" version="4.5.21" targetFramework="net461" />
<package id="NUnit" version="3.4.1" targetFramework="net461" />
</packages>
这些测试之前运行良好。我不知道发生了什么变化让他们现在抛出这个错误。
也许安装 4.6.1 .net Framework ?
请帮忙!
最佳答案
您的错误消息显示:
System.IO.FileNotFoundException : Impossible de charger le fichier ou l'assembly 'Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' ou une de ses dépendances. Le fichier spécifié est introuvable.
但是您的包具有以下配置:
<package id="Castle.Core" version="3.3.3" targetFramework="net461" />
- 您确定您的单元测试项目和代码都使用正确版本的程序集吗?
- 您的 App.config 文件中是否有任何程序集绑定(bind)重定向? https://stackoverflow.com/a/31013829/283787
- 它们都使用相同的 NuGet 包吗?
- 如果失败,您可以使用 FusionLogger 查看程序集的解析情况。
关于c# - Moq 单元测试 Moq.Proxy.CaSTLeProxyFactory 上的 : System. TypeInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39470613/