Mor*_*siu 1 c# terminal .net-core
我有类,它使用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解决问题.
以正常的方式......
创建一个描述您对控制台的访问权限的界面.在只使用System.Console的类中实现该接口.使用您最喜欢的"依赖注入"机制将其注入正在运行的代码中.
然后在测试期间创建另一个用于实现相同接口的类,但不使用System.Console类(例如,只需对WindowHeight和WindowWidth属性进行硬编码,让Console.ReadLine始终返回相同的字符串,编写Console.Write或WriteLine到其他地方).将该类注入单元测试代码.
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |