1

I would like to do two test cases that are evaluating on their boolean value.

public string ActivateAgent(bool trueorfalse)
        {
            agentActivationStatus = true;
            agentPriviledge = true;

            return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now active";
        }

I've only done unit tests with integer and strings, never a bool. This is the TestCase I tried to create:


[TestCase(true)]
        public void AgentIsActivated(bool expected)
        {
            bool result = Agent.ActivateAgent(bool true);
            Assert.AreEqual(expected, result);
        }

All I want to test is whether the agent is activated.

This is the full class for Agent, pretty basic but I'm new to C#.


public class Agent
    {
        string agentID;
       public bool agentActivationStatus = false; //activated yes no
        bool agentPriviledge = false; //has agent priviledges
        int waveStatus;
        public bool agentStatus = false;
        public DateTime agentLastSeen = DateTime.Now; //agent last seen alive
        public readonly string forename; //agent first name
        public readonly string surname; //agent last name;

        //CONSTRUCTORS --------------------------------------------------
        public Agent(string fname, string lname)
        {
            forename = fname;
            surname = lname;
            string fullname = fname + " " + lname;
        }

        //METHODS --------------------------------------------------------
        public string ActivateAgent(bool trueorfalse)
        {
            agentActivationStatus = true;
            agentPriviledge = true;

            return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now active";
        } 

        public string DeactivateAgent(bool trueorfalse)
        {
            agentActivationStatus = false;
            agentPriviledge = false;

            return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now unactive.";
        }

        public string getAgentStatus()
        {
            return $"Agent activation is {agentActivationStatus}, Agent is active";
        }

        //public string getAgentStatus()
        //{
        //    //string result = " ";
        //    //DateTime lastSeen30Days = agentLastSeen.AddDays(30);
        //    //if (agentLastSeen > lastSeen30Days && agentPriviledge ==)
        //    //{
        //    //   result = $"Agent was last seen {lastSeen30Days}, Agent is presumed MIA ";
        //    //}
        //    //else if (agentStatus == false && agentPriviledge == true)
        //    //{
        //    //    result = $"Agent was last seen {agentLastSeen} deceased.";
        //    //}
        //    //else
        //    //{
        //    //    result = $"Agent was last seen {agentLastSeen} Alive";
        //    //}
        //    //return result;
        }

2 Answers2

0

You can use IsTrue (or IsFalse)

[Test]
    public void AgentIsActivated()
    {
        bool activateAgent = true;
        bool result = Agent.ActivateAgent(activateAgent);
        Assert.IsTrue(result);
    }
ILally
  • 319
  • 1
  • 8
  • 13
  • `ActivateAgent(bool true)` is invalid C# syntax – Pavel Anikhouski Oct 20 '20 at 21:39
  • Thank you for the comment, but the line: ``` bool result = Agent.ActivateAgent(activateAgent); ``` "ActivateAgent" has a red squigly line underneath saying "An object reference is required for the non-static field, method, or property 'Agent.ActivateAgent(bool)' " – DriftingSands Oct 20 '20 at 21:59
  • @DriftingSands you have to create object `Agent` and you should call method on this object. – Lukasz Szczygielek Oct 20 '20 at 22:09
  • @Hostel, I'm not sure what you mean? Shouldn't I now be able to run the unit test? Why would I need to call it? I have added the full Agent class into the OP. – DriftingSands Oct 20 '20 at 22:14
  • @DriftingSands your method is public, not static, so you can call method on instance of the class. You should read about basics of C# programming, but also on SO you can find answer for it https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method – Lukasz Szczygielek Oct 20 '20 at 22:18
0

Your example code is almost good. TestCase is for tests which you want parametrized.

[TestCase(true)]
public void AgentIsActivated(bool expected)
{
    var agent = new Agent("abc", "def");
    bool result = agent.ActivateAgent(expected);
    Assert.AreEqual(expected, result);
}

If you don't need to pass parameter, then you can use Test attribute.

[Test]
public void AgentIsActivated()
{
    var agent = new Agent("abc", "def");
    bool result = agent.ActivateAgent(true);
    Assert.IsTrue(result);
}
Lukasz Szczygielek
  • 2,768
  • 2
  • 20
  • 34
  • Thank you for your help and I'm happy to hear that my code is close! However i'm getting the same problem as the example above you. I'm getting a "An object reference is required for the non-static field, method, or property 'Agent.ActivateAgent(bool)' " – DriftingSands Oct 20 '20 at 22:09
  • @DriftingSands you have to create object `Agent` and you should call method on this object. I don't know how your `Agent` class looks like. You should read about "How to create instance of class". – Lukasz Szczygielek Oct 20 '20 at 22:10