Писатель должен написать в stderrC#

Место общения программистов C#
Ответить
Anonymous
 Писатель должен написать в stderr

Сообщение Anonymous »

Код: Выделить всё

public static class Utils
{
/// 
/// Creates a Writer object that writes either to a file or to a console stream.
/// 
/// file name, "stdout" or "stderr"
/// if true, append to existing file; otherwise, overwrite
/// A TextWriter object that can be used to write output.
public static StreamWriter OutWriter(string outFname, bool ifAppend)
{
if (string.IsNullOrEmpty(outFname) || outFname == "stdout")
{
return new StreamWriter(Console.OpenStandardOutput());
}
else if (outFname == "stderr")
{
return new StreamWriter(Console.OpenStandardError());
}
else
{
FileStream fileStream;
if (ifAppend)
fileStream = new FileStream(outFname, FileMode.Append, FileAccess.Write, FileShare.None);
else
fileStream = new FileStream(outFname, FileMode.Create, FileAccess.Write, FileShare.None);
return new StreamWriter(fileStream, Encoding.UTF8);
}
}
}

Код: Выделить всё

[TestMethod]
public void OutWriter_ShouldReturnConsoleError_ForStderr()
{
// Capture the current Console.Error
TextWriter originalError = Console.Error;

// Redirect Console.Error to a StringWriter to capture the output
StringWriter errorWriter = new StringWriter();
Console.SetError(errorWriter);

using (StreamWriter writer = Utils.OutWriter("stderr", false))
{
// Write something to the StreamWriter
writer.WriteLine("Test error output");
writer.Flush(); // Ensure the output is flushed
}

// Reset the Console.Error to its original state
Console.SetError(originalError);

// Verify that the output was written to the StringWriter
string expectedOutput = "Test error output" + Environment.NewLine;
string actualOutput = errorWriter.ToString();
Assert.AreEqual(expectedOutput, actualOutput, "The writer should write to Console.Error.");
}
Вывод теста:

Код: Выделить всё

Test Name:  OutWriter_ShouldReturnConsoleError_ForStderr
Test FullName:  PdbLibTests.UtilsTests.OutWriter_ShouldReturnConsoleError_ForStderr
Test Source:    C:\git\MyProject_csharp\unit_test_for_Utils\UtilsTests.cs : line 15
Test Outcome:   Failed
Test Duration:  0:00:00.1644456

Result StackTrace:  at PdbLibTests.UtilsTests.OutWriter_ShouldReturnConsoleError_ForStderr() in C:\git\MyProject_csharp\unit_test_for_Utils\UtilsTests.cs:line 37
Result Message:
Assert.AreEqual failed. Expected:. Actual:. The writer should write to Console.Error.
Почему модульный тест не удается?
Как я могу добиться успеха?

Подробнее здесь: https://stackoverflow.com/questions/791 ... -to-stderr
Ответить

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

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

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

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

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