c# - Visual Studio 2015 单元测试不一致

标签 c# entity-framework unit-testing mstest visual-studio-2015

我在 VS2013 上移动到 VS2015 后收到不一致的单元测试结果。在 VS2013 上,单元测试始终通过。在 VS2015 中,如果同时运行所有单元测试或者它是自动化的 Jenkins 构建,单元测试将失败。如果单独运行单元测试或一次运行所有失败的测试,则它们通过。

这可能是一个 EF6 问题。运行所有测试时我收到的错误消息是...

Test Name:  GetQuoteService_NoExceptions
Test FullName:  MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions
Test Source:    C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\Unit Tests\MEA.SmartApp.Business.Factories.APV.UnitTests\QuoteFactoryTest.cs : line 15
Test Outcome:   Failed
Test Duration:  0:00:00.00737

结果堆栈跟踪:

at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
   at System.Data.Entity.Internal.AppConfig.<.ctor>b__2(ProviderElement e)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
   at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.RegisterDbProviderServices()
   at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
   at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetServiceAsServices(IDbDependencyResolver resolver, Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServices(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass6.<GetServices>b__5(IDbDependencyResolver r)
   at System.Linq.Enumerable.<SelectManyIterator>d__1`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__1`1.MoveNext()
   at System.Linq.Enumerable.<OfTypeIterator>d__1`1.MoveNext()
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.Infrastructure.DependencyResolution.InternalConfiguration.Lock()
   at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.<.ctor>b__1()
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.GetConfiguration()
   at System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
   at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
   at MEA.SmartApp.Contexts.APV.AVMDB..ctor() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\DataAccessLayer\MEA.SmartApp.Contexts.APV\AVMDB.Context.cs:line 19
   at MEA.SmartApp.Business.Factories.APV.QuoteFactory.GetAvmBusinessService() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\BusinessLogicLayer\MEA.SmartApp.Business.Factories.APV\QuoteFactory.cs:line 65
   at MEA.SmartApp.Business.Factories.APV.QuoteFactory.GetQuoteService(APVTransactionCode transactionCode) in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\BusinessLogicLayer\MEA.SmartApp.Business.Factories.APV\QuoteFactory.cs:line 46
   at MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\Unit Tests\MEA.SmartApp.Business.Factories.APV.UnitTests\QuoteFactoryTest.cs:line 18
Result Message: 
Test method MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions threw exception: 
**System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded**. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

*我意识到这不是真正的单元测试,因为它正在尝试连接到数据库,所以如果您对如何解决这个问题有任何建议,请告诉我。

最佳答案

@KeithPayne - 在用不同的其他单元测试集运行失败的单元测试后,我能够证明是另一个测试导致 Entity Framework 测试失败。所以你对并行执行的看法是正确的。

该单元测试有两个突出的属性导致 Entity Framework 单元测试失败。

  1. 单元测试涵盖了 VB.NET 代码,而我的测试套件中的所有其他测试都测试了 C# 代码。
  2. 单元测试最初是在 VS2008 中创建的,具有 DeploymentItem("MIGTRX.dll") 的测试属性。 MIGTRX 是被测试的 VB.NET 项目。使用此属性的原因是使用被测类的访问器类公开公开一个私有(private)方法,以便对其进行单元测试。 (注意:我意识到这是一种不好的做法,但这是我继承的遗留代码测试)

关于c# - Visual Studio 2015 单元测试不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712323/

相关文章:

c# - 切换分支后重新加载所有项目不会重新加载项目

c# - 有条件的分组、求和、相减

c# - 使用 Entity Framework 从表中选择所有字段

c# - 'System.Data.Entity.Internal.AppConfig' 的类型初始值设定项在子网站上抛出异常

c# - 如何中断或退出 Linq 查询

c# - wiimote中加速度计的刷新率是多少?

swift - Xcode Swift 单元测试输出过于冗长

node.js - 使用 mocha+sinon 测试 ExpressJS 路由时,如何 "stub"路由本地函数?

python - 如何模拟/修补整个类(class)?

c# - 组合/聚合许多底层服务响应的 WCF 路由器