单元测试的调用方法失败,并显示“不包含定义”错误

时间:2014-05-20 22:15:49

标签: c# visual-studio

我的代码中有些麻烦......请问你能帮助我吗?

我尝试在Visual Studio上创建一个测试单元,这是代码:

public partial class frmCalculator : Form
{
    string operand1 = string.Empty;
    string operand2 = string.Empty;
    string result;
    char operation;

    public frmCalculator()
    {
        InitializeComponent();
    }

    private void frmCalculator_Load(object sender, EventArgs e)
    {
        btnOne.Click += new EventHandler(btn_Click);
        btnTwo.Click += new EventHandler(btn_Click);
        btnThree.Click += new EventHandler(btn_Click);
        btnFour.Click += new EventHandler(btn_Click);
        btnFive.Click += new EventHandler(btn_Click);
        btnSix.Click += new EventHandler(btn_Click);
        btnSeven.Click += new EventHandler(btn_Click);
        btnEight.Click += new EventHandler(btn_Click);
        btnNine.Click += new EventHandler(btn_Click);
        btnZero.Click += new EventHandler(btn_Click);
        btnDot.Click += new EventHandler(btn_Click);
    }

    void btn_Click(object sender, EventArgs e)
    {
        try
        {
            Button btn = sender as Button;

            switch (btn.Name)
            {
                case "btnOne":
                    txtInput.Text += "1";
                    break;
                case "btnTwo":
                    txtInput.Text += "2";
                    break;
                case "btnThree":
                    txtInput.Text += "3";
                    break;
                case "btnFour":
                    txtInput.Text += "4";
                    break;
                case "btnFive":
                    txtInput.Text += "5";
                    break;
                case "btnSix":
                    txtInput.Text += "6";
                    break;
                case "btnSeven":
                    txtInput.Text += "7";
                    break;
                case "btnEight":
                    txtInput.Text += "8";
                    break;
                case "btnNine":
                    txtInput.Text += "9";
                    break;
                case "btnZero":
                    txtInput.Text += "0";
                    break;
                case "btnDot":
                    if(!txtInput.Text.Contains("."))
                        txtInput.Text += ".";
                    break;

            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details: " +
                ex.Message);
        }
    }

    private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
    {
        switch (e.KeyChar)
        {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
            //case '+':
            //case '-':
            //case '*':
            //case '/':
            //case '.':
                break;
            default:
                e.Handled = true;
                MessageBox.Show("Only numbers, +, -, ., *, / are allowed");
                break;
        }           
    }

    private void txtInput_TextChanged(object sender, EventArgs e)
    {

    }

    private void btnPlus_Click(object sender, EventArgs e)
    {
        operand1 = txtInput.Text;
        operation = '+';
        txtInput.Text = string.Empty;
    }

    private void btnMinus_Click(object sender, EventArgs e)
    {
        operand1 = txtInput.Text;
        operation = '-';
        txtInput.Text = string.Empty;
    }

    private void btnMulitply_Click(object sender, EventArgs e)
    {
        operand1 = txtInput.Text;
        operation = '*';
        txtInput.Text = string.Empty;
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        operand1 = txtInput.Text;
        operation = '/';
        txtInput.Text = string.Empty;
    }

    private void btnEqual_Click(object sender, EventArgs e)
    {

        operand2 = txtInput.Text;

        double opr1, opr2;
        double.TryParse(operand1, out opr1);
        double.TryParse(operand2, out opr2);

        switch (operation)
        {
            case '+':
                result = (opr1 + opr2).ToString();
                break;

            case '-':
                result = (opr1 - opr2).ToString();
                break;

            case '*':
                result = (opr1 * opr2).ToString();
                break;

            case '/':
                if (opr2 != 0)
                {
                    result = (opr1 / opr2).ToString();
                }
                else
                {
                    MessageBox.Show("Can't divide by zero");
                }
                break;
        }

        txtInput.Text = result.ToString();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtInput.Text = string.Empty;
        operand1 = string.Empty;
        operand2 = string.Empty;
    }

    private void btnSqrRoot_Click(object sender, EventArgs e)
    {
        double opr1;
        if (double.TryParse(txtInput.Text, out opr1))
        {
            txtInput.Text = (Math.Sqrt(opr1)).ToString();
        }
    }

    private void btnByTwo_Click(object sender, EventArgs e)
    {
        double opr1;
        if (double.TryParse(txtInput.Text, out opr1))
        {
            txtInput.Text = (opr1 / 2).ToString();
        }
    }

    private void btnByFour_Click(object sender, EventArgs e)
    {
        double opr1;
        if (double.TryParse(txtInput.Text, out opr1))
        {
            txtInput.Text = (opr1 / 4).ToString();
        }
    }




}

和我的班级考试:

[TestClass()]
public class frmCalculatorTests
{
    [TestMethod()]

    public void valor_limite_maximo(object sender, EventArgs e)
    {

        var calculadora = new frmCalculator();
        var retorno = calculadora.btnEqual_Click(123456789, 123456789);

        Assert.AreEqual(246913578, retorno);
    }
}

节目显示的信息是:

  

错误1'SimpleCalculator.frmCalculator'不包含'btnEqual_Click'的定义,并且没有可以找到接受类型'SimpleCalculator.frmCalculator'的第一个参数的扩展方法'btnEqual_Click'(你是否缺少using指令或程序集)参考?)C:\ Users \ milene \ Desktop \ nova calc-talvez certa \ Simple Calculator \ SimpleCalculatorTests \ frmCalculatorTests.cs 21 39 SimpleCalculatorTests

我如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这些方法标记为private,因此无法在表单范围之外访问。

答案 1 :(得分:1)

你可以使用 calculadora.btnEqual.PerformClick() 在此之前你必须初始化文本(这对夫妇(123456789,123456789)不是有效参数)。

<强>被修改 您必须将UI部分与业务逻辑分开。 例如: CBusinessLogic类     void OnBtnClick(String name)     {          开关(名字)          {              case&#34; btnOne&#34;:view.AddInput(&#34; 1&#34;)              等等          }     }

&#39;视图&#39;是CBusinessLogic的成员,是您的对象&#39; calculadora&#39;。创建表单后,您将创建CBusinessLogic类(例如在构造函数中)传递对象&#39; calculadora&#39;。表单还必须引用CBusinessLogic创建。

必须更改表单才能调用CBusinessLogic对象:

void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    businessLogic.OnBtnClick(btn.Name);
}

String GetInput()
{
    return txtInput;
}

void AddInput(String digit)
{
     txtInput.Text += digit;
}

这是MVC设计模式的应用。 有了它,您可以测试您的应用程序调用方法,如     字符串before = calculadora.GetInput();     businessLogic.OnBtnClick(&#34; 1&#34); 并且您可以验证输入是否已更改     字符串= calculadora.GetInput();     Assert.AreEqual(+&#34之前; 1&#34;之后);

您使用的参数是错误的,因为btnEqual_Click需要一个对象&#34; sender&#34; (可能是调用方法的表单或其他对象...确定不是整数)和click(EventArgs)的参数,而不是整数。 我想你想模拟&#34;添加操作&#34;的结果。您必须使用OnBtnClick(&#34; 1&#34;)然后OnBtnClick(&#34; 2&#34;)... OnBtnClick(&#34; 9&#34;)作为第一个数字。然后OnBtnPlusClick()。然后OnBtnClick(&#34; 1&#34;)...等第二个数字,然后是OnBtnEqual()。 此时,您可以检查结果是否符合预期。