Hi,
I have a test that have a Parallel.For in it. If I remove the Parallel.For, works fine, but if I try to do it in a parallel way I get an exception like if the Mocks where not applied.
Exception:
Test method Tgw.Wcs.StorageAndInventory.U.Test.Core.LoadCarrierManagerTest.UpdateLoadCarrierLocksLoadCarrier threw exception:
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: No connection string named 'StorageAndInventoryContext' could be found in the application config file.
Is like after the first thread is launched the rest of them cannot get the Mocked object...
I have a test that have a Parallel.For in it. If I remove the Parallel.For, works fine, but if I try to do it in a parallel way I get an exception like if the Mocks where not applied.
Exception:
Test method Tgw.Wcs.StorageAndInventory.U.Test.Core.LoadCarrierManagerTest.UpdateLoadCarrierLocksLoadCarrier threw exception:
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: No connection string named 'StorageAndInventoryContext' could be found in the application config file.
Is like after the first thread is launched the rest of them cannot get the Mocked object...
[TestInitialize] public void Initialize() { loadCarrierManager = new LoadCarrierManager(); loadCarrier = new LoadCarrier { Name = "LC1" }; //Creates a number of elements loadCarriers = CreateLoadCarriers(numberOfLoadcarriers); //First Mock the SIS Context storageAndInventoryContext = Mock.Create<StorageAndInventoryContext>(() => new StorageAndInventoryContext("Non"), Behavior.CallOriginal); //Mock the GetContext so it will return out Mocked element Mock.Arrange(() => StorageAndInventoryContextProvider.GetContext()) .Returns(storageAndInventoryContext); Mock.Arrange(() => storageAndInventoryContext.InventoryAccountSet.OfType<LoadCarrier>()) .ReturnsCollection(loadCarriers); } [TestMethod] [TestCategory(TgwTestingCategories.ServiceTest)] [WorkItem(11126)] public void UpdateLoadCarrierLocksLoadCarrier() { Parallel.For((long)0, 3, i => { using (var scope = new UnitOfWorkScope()) { for (int lcCount = numberOfLoadcarriers-1; lcCount > 0; lcCount--) { string lcName = string.Format("LC{0}", lcCount); LoadCarrierChangeSet loadCarrierChangeSet = new LoadCarrierChangeSet(); var lcToUpdate = loadCarrierManager.GetLoadCarrierData(lcName); loadCarrierChangeSet.Locks = new LockChangeSet("fooReason", "fooDescription"); loadCarrierManager.UpdateLoadCarrier(lcToUpdate.Name, loadCarrierChangeSet); } scope.Complete(); } }); using (new UnitOfWorkScope()) { var badEntityLocksDescriptions = storageAndInventoryContext.InventoryAccountSet.OfType<LoadCarrier>() .Select(x => x.EntityLocks.Where(y=>y.Description == "fooReason")); Assert.AreEqual(100, badEntityLocksDescriptions.Count()); } }