UnaultorizedAccessException не попадает в тест FakeIteasyC#

Место общения программистов C#
Ответить
Anonymous
 UnaultorizedAccessException не попадает в тест FakeIteasy

Сообщение Anonymous »

Я пишу модульный тест для метода, который должен поймать unauperizedAccessexception и вернуть специальный результат перечисления. Тем не менее, тест не сбои-метод возвращает успех вместо unauthorizedAccess .
[Theory]
[AutoData]
internal void GetDirectoryContent_WithNoAccess_ReturnUnauthorizedAccess(string directory)
{
//Arrange
A.CallTo(() => _directoryProxy.EnumerableDirectories(directory))
.Throws();
A.CallTo(() => _directoryProxy.EnumerateFiles(directory))
.Throws();
A.CallTo(() => _directoryProxy.Exists(directory))
.Returns(true);

var systemUnderTests = new Explorer(_loggerStub, _options, _directoryProxy);

//Act
var result = systemUnderTests.GetDirectoryContent(new FileEntity(directory, IsDirectory: true));

//Assert
result.Result.Should().Be(ExplorerResult.UnauthorizedAccess); // FAILS - returns Success
//If I try to get result.Entites will be get UnautorizedAccessException that I cauth in the explorer method
}
< /code>
класс в разделе «Тесты < /p>
internal sealed class Explorer(
ILogger logger,
IOptions explorerOptions,
IDirectoryProxy directoryProxy) : IExplorer
{
private const string LogicalDriveIsNotExistsLogMessage = "Logical drive: {drive} is not exists but was loaded, user possible has linux distro in dual-boot";
private const string GotDirectoryContentLogMessage = "Got {directory} content for {times} nanoseconds";
private const string LoadingAdditionalContentForLogicalDrives = "Loading additional content {path} for logical drives";
private const string DirectoryDoesNotExistLogMessage = "Directory {path} does not exists";

public (IEnumerable Entities, ExplorerResult Result) GetDirectoryContent(FileEntity entity)
{
var stopwatch = Stopwatch.StartNew();
var result = GetDirectoryContentWithoutTimer(entity);
stopwatch.Stop();

logger.LogDebug(GotDirectoryContentLogMessage, entity.Path, stopwatch.Elapsed.Nanoseconds);
return result;
}

private bool IsRootDirectory(string directoryPath) =>
directoryPath == explorerOptions.Value.RootDirectory;

private IEnumerable GetLogicalDrives()
{
foreach (var drive in directoryProxy.GetLogicalDrives())
{
if (IsLinuxLogicalDrive(drive))
{
logger.LogError(LogicalDriveIsNotExistsLogMessage, drive);
continue;
}

yield return new FileEntity(drive, IsDirectory: true);
}

foreach (var fileEntity in AddAdditionalLogicalDrivesContent())
yield return fileEntity;
}

private IEnumerable AddAdditionalLogicalDrivesContent() =>
explorerOptions.Value.RootDirectoryAdditionalContent
.Select(content =>
{
var path = Environment.ExpandEnvironmentVariables(content);
logger.LogDebug(LoadingAdditionalContentForLogicalDrives, path);
return new FileEntity(path, IsDirectory: true);
});

private IEnumerable GetDirectoryContentLazy(string path)
{
foreach (var directory in directoryProxy.EnumerableDirectories(path))
yield return new FileEntity(directory, IsDirectory: true);

foreach (var file in directoryProxy.EnumerateFiles(path))
yield return new FileEntity(file, IsDirectory: false);
}

private (IEnumerable, ExplorerResult) GetDirectoryContentWithoutTimer(FileEntity entity)
{
if (!entity.IsDirectory)
return ([], ExplorerResult.NotDirectory);

try
{
if (IsRootDirectory(entity.Path))
return (GetLogicalDrives(), ExplorerResult.Success);

if (!directoryProxy.Exists(entity.Path))
{
logger.LogError(DirectoryDoesNotExistLogMessage, entity.Path); //user renamed directory that shown in explorer view and try to open it
return ([], ExplorerResult.UnexistingDirectory);
}

return (GetDirectoryContentLazy(entity.Path), ExplorerResult.Success);
}
catch (UnauthorizedAccessException)
{
return ([], ExplorerResult.UnauthorizedAccess);
}
}

private bool IsLinuxLogicalDrive(string drive) => !directoryProxy.Exists(drive);
}
``


Подробнее здесь: https://stackoverflow.com/questions/797 ... teasy-test
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»