在因病休学 3 年后,我对编码总体来说还很陌生,并且我正在尝试为我正在从事的项目了解 C# 的基础知识。我的问题是使用 Windows 窗体应用程序的基本形状绘图程序,该程序使用命令/解析来输出所需的形状。我的程序按预期完美运行,但是当我尝试使用命令“绘制矩形 x y 宽度高度”为一个函数(绘制矩形)创建单元测试时,我遇到了我认为如何处理的问题我已经构建了我的代码,但我不确定。任何帮助将不胜感激,新年快乐! :-)
如果有人有时间简要查看一下我的代码的主要区域,我将在下面留下它:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace CommandParserApp
{
public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm mainForm = new MainForm();
Application.Run(mainForm);
}
}
public class MainForm : Form
{
//Declares the data stores for the program
private CommandParser commandParser;
private PictureBox canvasPictureBox;
private TextBox commandTextBox;
private ComboBox colorComboBox;
private Button executeButton;
private Button clearCanvasButton;
public MainForm() //Initializes the main form
{
InitializeComponent();
commandParser = new CommandParser();
}
private void InitializeComponent()
{
// Command TextBox that the user can enter commands into
commandTextBox = new TextBox
{
Location = new Point(10, 10),
Width = 300,
Multiline = true
};
// Color ComboBox that allows the user to select a colour for their shape
colorComboBox = new ComboBox
{
Location = new Point(320, 10),
Width = 80,
DropDownStyle = ComboBoxStyle.DropDownList //Uses a drop down list style on the combo box.
};
colorComboBox.Items.AddRange(new object[] { "Red", "Blue", "Green" }); //Adds the 3 colours to a range that the user can choose from.
colorComboBox.SelectedIndex = 0;
// Canvas PictureBox that is used to display shapes
canvasPictureBox = new PictureBox
{
Location = new Point(10, 50),
Size = new Size(400, 300),
BackColor = Color.White
};
// Instructions Label that informs the user of how to use the program
Label instructionsLabel = new Label
{
Location = new Point(10, 360),
AutoSize = true,
Text = "Instructions:\n" +
"To draw a rectangle: draw rectangle x y width height\n" +
"To draw a circle: draw circle x y diameter\n" +
"To draw a triangle: draw triangle x1 y1 x2 y2 x3 y3"
};
// Example Commands Label just to show the user examples of working commands
Label exampleCommandsLabel = new Label
{
Location = new Point(10, 440),
AutoSize = true,
Text = "Example Commands:\n" +
"Rectangle: draw rectangle 50 50 100 80\n" +
"Circle: draw circle 200 150 50 50\n" +
"Triangle: draw triangle 50 300 200 300 125 200"
};
// Execute Button that runs the command and is linked to the executeButton method
Button executeButton = new Button
{
Location = new Point(420, 10),
Text = "Execute"
};
executeButton.Click += ExecuteButton_Click;
// Clear Canvas Button, this allows the user to clear the canvas upon pressing the button
Button clearCanvasButton = new Button
{
Location = new Point(420, 50),
Text = "Clear Canvas"
};
clearCanvasButton.Click += ClearCanvasButton_Click;
Button loadButton = new Button
{
Location = new Point(420, 90),
Text = "Load"
};
loadButton.Click += LoadButton_Click; //Calls method when clicked
// Save Button that allows the user to save the contents of the input box to a txt file
Button saveButton = new Button
{
Location = new Point(420, 130),
Text = "Save"
};
saveButton.Click += SaveButton_Click;
// Adds the controls to the main form
Controls.Add(commandTextBox);
Controls.Add(colorComboBox);
Controls.Add(canvasPictureBox);
Controls.Add(instructionsLabel);
Controls.Add(exampleCommandsLabel);
Controls.Add(executeButton);
Controls.Add(clearCanvasButton);
Controls.Add(loadButton);
Controls.Add(saveButton);
}
private void ExecuteButton_Click(object sender, EventArgs e)
{
// Get the command from the TextBox
string command = commandTextBox.Text;
// Get the selected color from the ComboBox
Color selectedColor = GetSelectedColor();
// Parse and execute the command
try
{
commandParser.ExecuteCommand(command, selectedColor, canvasPictureBox.CreateGraphics());
}
catch (Exception ex) //Throws an error when command is invalid
{
MessageBox.Show($"Error: {ex.Message}", "Command Execution Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
下面是我的测试计划的代码,出现错误的代码区域是排列部分中的“MainForm mainForm = new MainForm():”部分。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;
[TestClass]
public class DrawingTests
{
[TestMethod]
public void DrawRectangleTest()
{
// Arrange
MainForm mainForm = new MainForm();
string command = "draw rectangle 50 50 50 50";
mainForm.commandTextBox.Text = command;
// Act
mainForm.ExecuteButton_Click(null, null);
// Assert
}
}
如果有人知道的话,谢谢您的帮助,毫无疑问,自从我上次尝试一些编码以来,我已经忘记了一些愚蠢的事情。
在与您的应用项目相同的解决方案中创建测试项目。
右键单击您的测试项目 -> 添加 -> 项目参考(或参考)
勾选您的应用程序项目的框,然后按确定。
通过在测试文件顶部添加“using”语句和应用程序项目的命名空间,将应用程序项目导入到测试项目中。在你的例子中它应该是
使用 CommandParserApp;
确保您的 MainForm 类是公共的(在您的代码中,它是公共的)