how to test bool response in csharp

To test a bool response in C#, one can use a testing framework like NUnit.

Assuming the bool response is returned from a method, one can write a test method to assert the expected value of the bool response.

main.cs
using NUnit.Framework;

[TestFixture]
public class MyTests
{
    [Test]
    public void TestBoolResponse()
    {
        MyClass myClassInstance = new MyClass();
        bool actualResult = myClassInstance.MyBoolMethod();
        bool expectedResult = true;

        Assert.AreEqual(expectedResult, actualResult, "Bool response not as expected");
    }
}
355 chars
16 lines

In the above code, we have written a test method TestBoolResponse which creates an instance of our class MyClass, calls a method MyBoolMethod() which returns a bool response. We are then initializing our expected bool response expectedResult to true.

The last line in the method does the actual assertion, comparing the expected and actual bool response values. If the values are not equal, it will throw an error with message "Bool response not as expected".

gistlibby LogSnag