我遇到了这种奇怪的情况,如果我一起运行所有测试,某些测试将失败(大约7个)。但是如果我只运行类中的测试(它们都属于同一个类),那么它们就会通过。测试项目是Windows Phone 8.1 MSTest,我尝试使用Resharper测试运行器和MSTest测试运行器运行它们,它们都显示相同的问题。这是我的TestInitialize代码:
[TestInitialize]
public void Init()
{
ResolveDependencies();
var adsApiService = ServiceLocator
.Current
.GetInstance<IApiService<ListAdsReply, PublicAdsEndPoint>>();
var navigationService = new NavigationServiceMock();
var mainPageTrackingService = ServiceLocator
.Current
.GetInstance<IMainPageTrackingService>();
var adInsertionTrackingService = ServiceLocator
.Current
.GetInstance<IAdInsertionTrackingService>();
var connectionService = ServiceLocator
.Current
.GetInstance<IConnectionService>();
_windowsApiService = new WindowsApiServiceMock();
var contactAboutTrackingService = ServiceLocator
.Current
.GetInstance<IContactAboutTrackingService>();
var filtersTrackingService = ServiceLocator
.Current
.GetInstance<IFiltersTrackingService>();
var filtersService = ServiceLocator
.Current
.GetInstance<IFiltersService>();
var messageHelperMock = new MessageHelperMock();
_mainPageViewModel = new MainPageViewModel(adsApiService, navigationService, mainPageTrackingService, adInsertionTrackingService, connectionService, _windowsApiService, contactAboutTrackingService,filtersTrackingService, filtersService, messageHelperMock);
}
除了在Unity容器中注册依赖项,使用ResolveDependencies
注册它并执行一些Automapper配置之外,ServiceLocator.SetLocatorProvider
方法没有做任何特殊操作。那里没有异步代码。
[TestMethod]
public async Task GivenParameterIsProvidedThenFetchDataShouldReturnValidData()
{
_mainPageViewModel
.SearchParams
.Add(new KeyValuePair<string, string>("lim", "5"));
var searchParams = _mainPageViewModel.SearchParams;
await _mainPageViewModel.FetchData(searchParams);
var list = _mainPageViewModel.AdsList;
Assert.IsNotNull(list);
}
这是失败的测试之一。
public async Task<ListAdsReplyViewModel> FetchData(List<KeyValuePair<string, string>> parameters)
{
_cancellationTokenSource = new CancellationTokenSource();
_cancellationTokenSource
.CancelAfter(Constants.TimeToCancelAsyncOperations);
AddSearchKeywordToSearchParams();
var result = await _listAdsReplyApiService
.GetWithParametersAsync(_cancellationTokenSource, parameters);
var vm = new ListAdsReplyViewModel
{
Ads = new List<AdInfoViewModel>()
};
foreach (var listAd in result.ListAds)
{
var listAdDto = Mapper.Map<ListAdDto>(listAd);
var adInfo = new AdInfoViewModel(_navigationService, _mainPageTrackingService)
{
ListAdDto = listAdDto
};
vm.Ads.Add(adInfo);
}
vm.NextPage = result.NextPage;
vm.ConfigEtag = result.ConfigEtag;
vm.Sorting = result.Sorting;
TotalAds = result.ListAdsCounterMap.AllAds;
return vm;
}
private void AddSearchKeywordToSearchParams()
{
if (!string.IsNullOrEmpty(SearchKeyWord))
{
var searchKeyword = SearchParams
.FirstOrDefault(x => x.Key == "q");
if (!searchKeyword.Equals(null))
SearchParams.Remove(searchKeyword);
SearchParams.Add(new KeyValuePair<string, string>("q", SearchKeyWord));
}
}
这是测试中的方法。调用foreach((var listAd in result.ListAds)
时似乎发生了这个问题。这就像GetWithParametersAsync(_cancellationTokenSource, parameters);
没有被等待,因为我对失败的测试得到以下错误:
Test method App.Tests.Integration.App.Shared.ViewModels.MainPageViewModelTests.GivenParameterIsProvidedThenFetchDataShouldReturnValidData threw exception:
AutoMapper.AutoMapperMappingException:
Mapping types:
ListAd -> ListAdDto
Core.Api.Models.PublicAds.ListAd -> Core.Dto.ListAdDto
Destination path:
ListAdDto
Source value:
Core.Api.Models.PublicAds.ListAd ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Core.Bootstrap.AutoMapperConfiguration.<>c__DisplayClass0_0.<Configure>b__12(ListAd src, ListAdDto dest)
at AutoMapper.Internal.MappingExpression`2.<>c__DisplayClass57_0.<AfterMap>b__0(Object src, Object dest)
at AutoMapper.TypeMap.<get_AfterMap>b__40_0(Object src, Object dest)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
--- End of inner exception stack trace ---
at App.ViewModels.MainPageViewModel.<FetchData>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App.Tests.Integration.App.Shared.ViewModels.MainPageViewModelTests.<GivenParameterIsProvidedThenFetchDataShouldReturnValidData>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
有什么想法吗?
答案 0 :(得分:2)
由于评论中已接受这一点,我已将其重新发布为答案;
&#34;您是否考虑过MSTest将运行您的测试多线程 - 并且通过执行单个测试您正在强制使用单线程?要非常小心,所有测试都是UNIT测试,并且在执行顺序中没有依赖关系,或者共享资源可能已经被并行运行的另一个测试实例化(或处于实例化过程中)的可能性。 &#34;