Question:
Write unit test case for ternary condition using MSTest C# .Net 6

Most unit test frameworks allow passing data to unit tests. In MSTest this is done using the DataRow attribute. You can pass different value combinations for campaign and status and check whether the resulting object's properties match what you expected.


You can test the method for different Campaign ID values like this


[TestMethod]

[DataRow("","DefaultID")]

[DataRow(null,"DefaultID")]

[DataRow("1234","1234")]

public void Update_For_CampId(string? campId,string expectedCampId)

{

    var transaction=new Transaction()

    {

        CampaignId="DefaultID",

        Status="DefaultStatus"

    };

    var argument = new Transaction.CreateArguments();

    

    argument.CampaignId=campId;

    argument.Status="SomeStatus";


    var response = Transaction.Update(transaction, argument);


    Assert.AreEqual(expectedCampId,response.CampaignId);

    Assert.AreEqual(expectedStatus,response.Status);

}


Testing Status is similar :

[TestMethod]

[DataRow("","DefaultStatus")]

[DataRow(null,"DefaultStatus")]

[DataRow("OK","OK")]

public void Update_For_Status(string? status, string expectedStatus)

{

    var transaction=new Transaction()

    {

        CampaignId="DefaultID",

        Status="DefaultStatus"

    };

    var argument = new Transaction.CreateArguments();

    

    argument.CampaignId="SomeID";

    argument.Status=status;


    var response = Transaction.Update(transaction, argument);


    Assert.AreEqual(expectedCampId,response.CampaignId);

    Assert.AreEqual(expectedStatus,response.Status);

}


Answered by:> Panagiotis Kanavos

Credit:> Stack Overflow


Read more:

>Built Web API using ASP.NET (C#)

>Plugins and Presets for Vuejs project

>How to Create an array based on two other arrays in Php

>Testing react components using Hooks and Mocks

>Use Firebase Realtime Database with ASP.NET MVC App

>Setting up a Cloud Composer environment: Step-by-step guide


Ritu Singh

Ritu Singh

Submit
0 Answers