我正在使用xUnit 2.x和moq 4.5以及castle.core 3.3.0.0。我的IAssessmentsRepository接口位于不同的程序集中(但是相同解决方案的一部分),而不是我的单元测试。我正在引用IAssessmentsRepository所在的程序集。但是当我尝试模拟时,它会出现“类型初始化”的错误,这意味着moq无法加载类型,即IAssessmentsRepository。现在我不明白为什么?如果有人遇到同样的问题,请提出建议。
[Fact]
[UnitTest]
public void DeleteAssessment_ShouldCall_AssessmentsRepository_DeleteAssessment_Method()
{
//Arrange
var assessmentRepoMock = new Mock<Data.Assessments.IAssessmentsRepository>();
assessmentRepoMock.Setup(x => x.DeleteAssessment(It.IsAny<int>(), It.IsAny<string>())).Returns(true);
var assessmentService = new AssessmentService(assessmentRepoMock.Object);
//Act
var testResult = assessmentService.DeleteAssessment(It.IsAny<int>(), It.IsAny<string>());
//Assert
assessmentRepoMock.Verify(r => r.DeleteAssessment(It.IsAny<int>(), It.IsAny<string>()), Times.Once);
}
以下是错误的堆栈跟踪
UHCNV.Web.Services.Icm.Services.Tests.UnitTests.AssessmentServiceTests.DeleteAssessment_ShouldCall_AssessmentsRepository_DeleteAssessment_Method [FAIL]
[xUnit.net 00:00:07.6027020] System.TypeInitializationException : The type initializer for 'Moq.Mock`1' threw an exception.
[xUnit.net 00:00:07.6033988] ---- System.TypeInitializationException : The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception.
[xUnit.net 00:00:07.6038240] -------- System.Configuration.ConfigurationErrorsException : Configuration system failed to initialize
[xUnit.net 00:00:07.6042223] ------------ System.Configuration.ConfigurationErrorsException : Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\DEV2\LouisFinal\UpdatedOne\UHCNV.WEB.SERVICES.ICM\test\UHCNV.Web.Services.Icm.Services.Tests\bin\x86\Debug\UHCNV.Web.Services.Icm.Services.Tests.dll.config line 23)
[xUnit.net 00:00:07.6113488] Stack Trace:
[xUnit.net 00:00:07.6119217] at Moq.Mock`1.<InitializeInstance>b__24_0()
[xUnit.net 00:00:07.6123448] at Moq.PexProtector.Invoke(Action action)
[xUnit.net 00:00:07.6127299] at Moq.Mock`1.InitializeInstance()
[xUnit.net 00:00:07.6131116] at Moq.Mock`1.OnGetObject()
[xUnit.net 00:00:07.6134937] at Moq.Mock.GetObject()
[xUnit.net 00:00:07.6138850] at Moq.Mock.get_Object()
[xUnit.net 00:00:07.6142657] at Moq.Mock`1.get_Object()
[xUnit.net 00:00:07.6146855] test\UHCNV.Web.Services.Icm.Services.Tests\UnitTests\AssessmentServiceTests.cs(20,0): at UHCNV.Web.Services.Icm.Services.Tests.UnitTests.AssessmentServiceTests.DeleteAssessment_ShouldCall_AssessmentsRepository_DeleteAssessment_Method()
[xUnit.net 00:00:07.6150651] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6154568] at Moq.Proxy.CastleProxyFactory..ctor()
[xUnit.net 00:00:07.6158157] at Moq.Mock`1..cctor()
[xUnit.net 00:00:07.6161683] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6165556] at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
[xUnit.net 00:00:07.6169380] at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
[xUnit.net 00:00:07.6173346] at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
[xUnit.net 00:00:07.6177103] at System.Configuration.ConfigurationManager.GetSection(String sectionName)
[xUnit.net 00:00:07.6180813] at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
[xUnit.net 00:00:07.6184595] at System.Diagnostics.DiagnosticsConfiguration.Initialize()
[xUnit.net 00:00:07.6188321] at System.Diagnostics.DiagnosticsConfiguration.get_Sources()
[xUnit.net 00:00:07.6191972] at System.Diagnostics.TraceSource.Initialize()
[xUnit.net 00:00:07.6195693] at Castle.Core.Logging.TraceLogger.IsSourceConfigured(TraceSource source)
[xUnit.net 00:00:07.6199486] at Castle.Core.Logging.TraceLogger.Initialize()
[xUnit.net 00:00:07.6204199] at Castle.Core.Logging.TraceLogger..ctor(String name, LoggerLevel level)
[xUnit.net 00:00:07.6208012] at Castle.DynamicProxy.ProxyGenerator..ctor(IProxyBuilder builder)
[xUnit.net 00:00:07.6211584] at Castle.DynamicProxy.ProxyGenerator..ctor()
[xUnit.net 00:00:07.6215447] at Moq.Proxy.CastleProxyFactory.CreateProxyGenerator()
[xUnit.net 00:00:07.6219298] at Moq.Proxy.CastleProxyFactory..cctor()
[xUnit.net 00:00:07.6222921] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6226725] at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
[xUnit.net 00:00:07.6230535] at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
[xUnit.net 00:00:07.6242699] at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
[xUnit.net 00:00:07.6247423] at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
[xUnit.net 00:00:07.6633781] UHCNV.Web.Services.Icm.Services.Tests.UnitTests.AssessmentServiceTests.GenerateAssessmentForm_ShouldCall_AssessmentsRepository_GenerateAssessmentForm_Method [FAIL]
[xUnit.net 00:00:07.6715323] System.TypeInitializationException : The type initializer for 'Moq.Mock`1' threw an exception.
[xUnit.net 00:00:07.6719637] ---- System.TypeInitializationException : The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception.
[xUnit.net 00:00:07.6723726] -------- System.Configuration.ConfigurationErrorsException : Configuration system failed to initialize
[xUnit.net 00:00:07.6727605] ------------ System.Configuration.ConfigurationErrorsException : Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\DEV2\LouisFinal\UpdatedOne\UHCNV.WEB.SERVICES.ICM\test\UHCNV.Web.Services.Icm.Services.Tests\bin\x86\Debug\UHCNV.Web.Services.Icm.Services.Tests.dll.config line 23)
[xUnit.net 00:00:07.6796134] Stack Trace:
[xUnit.net 00:00:07.6801181] at Moq.Mock`1.<InitializeInstance>b__24_0()
[xUnit.net 00:00:07.6805315] at Moq.PexProtector.Invoke(Action action)
[xUnit.net 00:00:07.6809086] at Moq.Mock`1.InitializeInstance()
[xUnit.net 00:00:07.6813035] at Moq.Mock`1.OnGetObject()
[xUnit.net 00:00:07.6816928] at Moq.Mock.GetObject()
[xUnit.net 00:00:07.6820733] at Moq.Mock.get_Object()
[xUnit.net 00:00:07.6824593] at Moq.Mock`1.get_Object()
[xUnit.net 00:00:07.6828478] test\UHCNV.Web.Services.Icm.Services.Tests\UnitTests\AssessmentServiceTests.cs(40,0): at UHCNV.Web.Services.Icm.Services.Tests.UnitTests.AssessmentServiceTests.GenerateAssessmentForm_ShouldCall_AssessmentsRepository_GenerateAssessmentForm_Method()
[xUnit.net 00:00:07.6832594] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6836425] at Moq.Proxy.CastleProxyFactory..ctor()
[xUnit.net 00:00:07.6841577] at Moq.Mock`1..cctor()
[xUnit.net 00:00:07.6845556] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6856442] at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
[xUnit.net 00:00:07.6860674] at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
[xUnit.net 00:00:07.6864632] at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
[xUnit.net 00:00:07.6869201] at System.Configuration.ConfigurationManager.GetSection(String sectionName)
[xUnit.net 00:00:07.6873166] at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
[xUnit.net 00:00:07.6877093] at System.Diagnostics.DiagnosticsConfiguration.Initialize()
[xUnit.net 00:00:07.6881252] at System.Diagnostics.DiagnosticsConfiguration.get_Sources()
[xUnit.net 00:00:07.6885259] at System.Diagnostics.TraceSource.Initialize()
[xUnit.net 00:00:07.6889303] at Castle.Core.Logging.TraceLogger.IsSourceConfigured(TraceSource source)
[xUnit.net 00:00:07.6893368] at Castle.Core.Logging.TraceLogger.Initialize()
[xUnit.net 00:00:07.6897193] at Castle.Core.Logging.TraceLogger..ctor(String name, LoggerLevel level)
[xUnit.net 00:00:07.6901234] at Castle.DynamicProxy.ProxyGenerator..ctor(IProxyBuilder builder)
[xUnit.net 00:00:07.6905143] at Castle.DynamicProxy.ProxyGenerator..ctor()
[xUnit.net 00:00:07.6909231] at Moq.Proxy.CastleProxyFactory.CreateProxyGenerator()
[xUnit.net 00:00:07.6913291] at Moq.Proxy.CastleProxyFactory..cctor()
[xUnit.net 00:00:07.6917210] ----- Inner Stack Trace -----
[xUnit.net 00:00:07.6921119] at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
[xUnit.net 00:00:07.6925092] at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
[xUnit.net 00:00:07.6929139] at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
[xUnit.net 00:00:07.6933131] at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
[xUnit.net 00:00:07.7185612] Finished: UHCNV.Web.Services.Icm.Services.Tests
答案 0 :(得分:2)
我找到了这个问题的根本原因。它与测试项目中的app.config文件有关。我的app.config是(当我面临问题时),如下所示,
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
我需要做的就是确保应该是我的根元素的第一个孩子,所以我更新了我的app.config文件,如下所示,
<configuration>
<configSections>
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
和volla,问题解决了。错误信息令人困惑。