-2

I'm creating a unit test for an existing project.

  • n1 and n2 are input numbers
  • op is operands in a switch case in the main program

The problem is with actual. I cannot match the expected and actual value because I get the error cannot implicitly convert void to int.

My unit test:

[TestMethod()]
public void docalcTest(int actual)
{
    Form1 target = new Form1(); // TODO: Passenden Wert initialisieren

    double n1 = 15; // TODO: Passenden Wert initialisieren
    double n2 = 3; // TODO: Passenden Wert initialisieren
    int op = 2; // TODO: Passenden Wert initialisieren
    int expected = 5;

    actual = target.docalc(n1, n2, op);

    Assert.AreEqual(expected,actual);
}

The code for docalc:

public void docalc(double n1, double n2, int op)
{
    result = 0;
    setText("clear");

    switch (op)
    {
        case 1:
            result = n1 + n2;
            break;
        case 2:
            result = n1 - n2;
            break;
        case 3:
            result = n1 * n2;
            break;
        case 4:
            result = n1 / n2;
            break;
    }

    setText(result.ToString());
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Ruud
  • 39
  • 1
  • 3
  • 10
  • 5
    What is 'target.docalc' ? – PhonicUK Jun 12 '13 at 13:31
  • 2
    What does `docalc` return? It can't be a `void` method if you want it to return anything. – Oded Jun 12 '13 at 13:31
  • What does target.docalc(...) return? I'm guessing void which is why you're getting this error – Dave Hogan Jun 12 '13 at 13:31
  • 2
    There is no point in passing `actual` into the method if you are going to assign something to it before you use the provided value. – Dan Jun 12 '13 at 13:33
  • 'public void docalc(double n1, double n2, int op) { result = 0; setText("clear"); switch (op) { case 1: result = n1 + n2; break; case 2: result = n1 - n2; break; case 3: result = n1 * n2; break; case 4: result = n1 / n2; break; } setText(result.ToString()); }' – Ruud Jun 12 '13 at 13:33
  • @Ruud your method doesn't return anything. – Daniel A. White Jun 12 '13 at 13:33
  • the code for docalc from main project. does basic plus minus multiply division operations. – Ruud Jun 12 '13 at 13:35
  • 1
    @Ruud: `docalc` appears to have a serious design flaw. It doesn't actually return the result of its calculation, it calls another function which is undoubtedly UI-bound to display the result of the calculation. This is tightly-coupled with the UI and can't be tested in isolation (which is what you're attempting to do). The business logic should be de-coupled from the UI so it can be tested. – David Jun 12 '13 at 13:36

1 Answers1

4

Your method target.docalc() is a void method, while actual is an int. You can't assign void to an int, as the compiler says.

According to your comment (you really should just edit your question), your docalc() looks like this:

public void docalc(double n1, double n2, int op) 
{   
    result = 0; 

    ...

    setText(result.ToString());
}

You'll have to change the return type of the method to int, and return result:

public int docalc(double n1, double n2, int op) 
{   
    int result = 0; 

    ...

    return result;
}

Sidenote, why do you do this?

[TestMethod()]
public void docalcTest(int actual)
{
     ...

    actual = ...

The test method will be called without parameters, so it's a bit useless there. You might want to change it to:

[TestMethod()]
public void docalcTest()
{
     ...

    int actual = ...
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I know i can't use void with int. while doing '[TestMethod()] public void docalcTest() { ... int actual = ...' also doesn't work. tried already, the same error. that's why looking the way. thanks in advance – Ruud Jun 12 '13 at 13:38
  • @Ruud read my answer again. The remark about the declaration of `int actual` is not the answer, changing the return type of the `docalc()` method is. – CodeCaster Jun 12 '13 at 13:39
  • Ok, got it. Thanks. that means should change whole code now :) – Ruud Jun 12 '13 at 14:03
  • @Ruud that is correct. You should create a separate class to do the calculations, not let a form do that. Then you instantiate a new instance of that class where you need it. – CodeCaster Jun 12 '13 at 14:14
  • 1
    thanks for help. with some slight changes solved my problem – Ruud Jun 13 '13 at 12:57