相关疑难解决方法(0)

在单元测试中检查控制台输出

有没有办法可以检查抽象类问题的单元测试中控制台的输出是什么?

我正在使用 NUnit 和 Moq。

我的单元测试如下所示:

    [Test]
    public void QuestionAsk()
    {
        var mock = new Mock<Question>(new object[]{"question text",true});

        mock.CallBase = true;

        var Question = mock.Object;

        Question.Ask();

        mock.Verify(m => m.Ask(), Times.Exactly(1));

    }
Run Code Online (Sandbox Code Playgroud)

在这里,我检查 Question.Ask() 是否被调用并且工作正常。Ask() 不返回值,因此我无法将其分配给变量。该函数仅输出到控制台。

有没有办法在测试中验证输出==“问题文本”?

编辑:忘记提及 Question 是一个抽象基类。

我尝试了使用此代码建议的 Concole.Setout 方法:

    [Test]
    public void QuestionAsk()
    {
        var mock = new Mock<Question>(new object[]{"question text",true});

        mock.CallBase = true;

        var Question = mock.Object;

        using (var consoleText = new StringWriter())
        {
            Console.SetOut(consoleText);
            Question.Ask();
            Assert.That(consoleText.ToString(), Is.StringMatching("question text"));
        }
        mock.Verify(m => m.Ask(), Times.Exactly(1));

    }
Run Code Online (Sandbox Code Playgroud)

但花费了 …

c# testing nunit unit-testing moq

4
推荐指数
1
解决办法
2万
查看次数

这可以用Moq嘲笑吗?

我正在努力模拟一些外部依赖项,并且遇到一个第三方类的问题,它将构造函数作为另一个第三方类的实例.希望SO社区可以给我一些指导.

我想创建一个模拟实例,SomeRelatedLibraryClass它接受它的构造函数的一个模拟实例SomeLibraryClass.我怎么能这样嘲笑SomeRelatedLibraryClass

回购代码......

这是我在测试控制台应用程序中使用的Main方法.

public static void Main()
{
    try
    {
        SomeLibraryClass slc = new SomeLibraryClass("direct to 3rd party");
        slc.WriteMessage("3rd party message");
        Console.WriteLine();

        MyClass mc = new MyClass("through myclass");
        mc.WriteMessage("myclass message");
        Console.WriteLine();

        Mock<MyClass> mockMc = new Mock<MyClass>("mock myclass");
        mockMc.Setup(i => i.WriteMessage(It.IsAny<string>()))
            .Callback((string message) => Console.WriteLine(string.Concat("Mock SomeLibraryClass WriteMessage: ", message)));

        mockMc.Object.WriteMessage("mock message");
        Console.WriteLine();
    }
    catch (Exception e)
    {
        string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
        Console.WriteLine(error);
    }
    finally
    {
        Console.Write("Press …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing moq mocking

3
推荐指数
1
解决办法
2194
查看次数

如何对使用Console类的类进行单元测试?

我有类,它使用Console类并在构造函数中设置此变量.

  public Scene() {
        Height = Console.WindowHeight;
        Width = Console.WindowWidth;
    } 
Run Code Online (Sandbox Code Playgroud)

和测试类,它测试这个构造函数:

public class SceneTests {
    [Fact]
    public void Contructor_None_ShouldReturnScene() {
        var testScene = new Scene();
        Assert.Equal(Console.WindowHeight, testScene.Height);
        Assert.Equal(Console.WindowWidth, testScene.Width);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在运行这个测试的时候,我有了Exeption:

Exception has occurred: CLR/System.IO.IOException
An exception of type 'System.IO.IOException' occurred in System.Console.dll but was not handled in user code: 'Nieprawid?owe doj?cie'
at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
at System.ConsolePal.get_WindowHeight()
at System.Console.get_WindowHeight()
Run Code Online (Sandbox Code Playgroud)

我想,那是因为我没有实际的Console窗口.

我可以以某种方式模拟那个吗?也许简单地手动创建缓冲区并添加它将Console解决问题.

c# terminal .net-core

1
推荐指数
1
解决办法
92
查看次数

标签 统计

c# ×3

moq ×2

unit-testing ×2

.net ×1

.net-core ×1

mocking ×1

nunit ×1

terminal ×1

testing ×1