Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use NUnit to test unit and I have a method:

C#
class abc
   {
      private int a;
      public void myMethod()
      {
           if(MessageBox.Show("Sure?","Title", MessageBoxButtons.YesNo) == DialogResult.Yes)
                 a = 1;
           else
                 a = 0;
      }
   }


How do I write NUnit to test this method? Sorry for my English.
Thank you very much.
Posted
Comments
Sergey Alexandrovich Kryukov 12-May-15 2:15am    
Read NUnit documentation; development of tests is fairly simple. What exactly do you want to test?
—SA
Trung Tuyen Tran 12-May-15 2:20am    
Hi Sergey Alexandrovich Kryukov,
When I call the method in TestProject , It show a messagebox and I should click button Yes or No . I want auto set value for MessageBox. Can I do that?
Thank you.
Sergey Alexandrovich Kryukov 12-May-15 2:25am    
Not clear. Why? Please describe the whole idea and the problem from the very beginning to the end.
—SA
Trung Tuyen Tran 12-May-15 2:47am    
this is my class and method :

class abc
{
public int myMethod()
{
if(MessageBox.Show("Sure?","Title", MessageBoxButtons.YesNo) == DialogResult.Yes)
return 1;
else
return 0;
}
}

And this is my TestMethod

[Test]
public void TestMyMethod()
{
abc x = new abc();
int expect = 1;
int actual = x.myMethod();
Assert.AreEqual(expect, actual);
}
When I run TestMyMethod() . It had a messagebox . I must click button YES(DialogResult.Yes) or button NO (DialogResult.No) . I don't want show the messagebox . I want auto set value is DialogResult.Yes

1 solution

You could have a look at NUnitForms[^] or change myMethod to take a flag to say whether or not to show the message e.g.
C#
public int myMethod(bool ShowMessage = true)
{
    int retVal = 1;
    if (ShowMessage)
        retVal = (MessageBox.Show("Sure?", "Title", MessageBoxButtons.YesNo) == DialogResult.Yes) ? 1 : 0;

    return retVal;
}

Note however, changing the application to suit the test in this way isn't recommended.
 
Share this answer
 
Comments
Trung Tuyen Tran 12-May-15 6:10am    
Hi Chill60,

Thank you so much but this is function of another people . I'm not allowed to change it. And don't allowed to use NUnitForm . So do you have an other solution? .

Thank you so much
CHill60 12-May-15 7:25am    
If you are allowed to use NUnit why are you not allowed to use the extension NUnitForms? Seems strange.
However, the only other way I can think of is to inject a replacement for the MessageBox functionality - I found this SO post[^] discusses that method
Philippe Mori 12-May-15 19:57pm    
If you are not allowed to do anything, then we cannot help you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900