public void MakeSureEnumeratorUnregistersItselWhenLeftUndisposed() { var mockRepository = new MockRepository(); IFolderEnumeratorRegistry registryMock = mockRepository.DynamicMock <IFolderEnumeratorRegistry>(); IFilesAndFoldersProvider fileProviderStub = mockRepository.Stub <IFilesAndFoldersProvider>(); var folderInfo = new FolderInfo(new FolderNode("dsf", Guid.NewGuid(), 1, 2, new DataStreamDefinition(12, 0), new DataStreamDefinition(11, 0), DateTime.UtcNow, Guid.NewGuid()), "\\folder"); var enumerator = new FolderContentsEnumerator(folderInfo, "*", registryMock, fileProviderStub); using (mockRepository.Unordered()) { registryMock.Unregister(null); LastCall.IgnoreArguments().Repeat.Once(); } mockRepository.ReplayAll(); enumerator = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); mockRepository.VerifyAll(); }
/// <summary> /// /// </summary> /// <param name="folderToEnumerateFilesIn"></param> /// <param name="patternForMatchingNamesOfFiles"></param> /// <param name="enumeratorRegistry"></param> /// <param name="filesAndFoldersProvider"></param> /// <exception cref="ArgumentNullException"></exception> internal FolderContentsEnumerator(FolderInfo folderToEnumerateFilesIn, string patternForMatchingNamesOfFiles, IFolderEnumeratorUnregistrar enumeratorRegistry, IFilesAndFoldersProvider filesAndFoldersProvider) { try { if (folderToEnumerateFilesIn == null) { throw new ArgumentNullException("folderToEnumerateFilesIn"); } if (enumeratorRegistry == null) { throw new ArgumentNullException("enumeratorRegistry"); } if (filesAndFoldersProvider == null) { throw new ArgumentNullException("filesAndFoldersProvider"); } if (String.IsNullOrEmpty(patternForMatchingNamesOfFiles)) { throw new ArgumentNullException("patternForMatchingNamesOfFiles"); } _folderToEnumerateFilesIn = folderToEnumerateFilesIn; _patternForMatchingNamesOfFiles = patternForMatchingNamesOfFiles; _enumeratorRegistry = enumeratorRegistry; _filesAndFoldersProvider = filesAndFoldersProvider; _wildcardForMatchingNamesOfFiles = new Wildcard(patternForMatchingNamesOfFiles); _currentFolder = folderToEnumerateFilesIn; } catch (Exception) { GC.SuppressFinalize(this); // Finalizer вызывается даже если возникло исключение при конструировании объекта. throw; } }