0

I have a public void method, SaveCover2AData(). How can I create a unit test for this type of method?

This is my public method, I am passing a model object as a parameter in this method.

Now I want to create unit test for this method:

public void SaveCover2AData(CASTabCover2a t2a)
{
    CASCreateViewModel obj = (CASCreateViewModel)Session["CASQuote"];

    t2a.Quote_Exposures = t2a.Quote_Exposures.Where(x => x.Exposure != null || x.ExposurePL != null).ToList();
    t2a.Quote_Jurisdictions = t2a.Quote_Jurisdictions.Where(x => x.Jurisdiction_Name != null || x.Country_NamePL != null).ToList();
    t2a.Quote_LimtOfIndemnitys = t2a.Quote_LimtOfIndemnitys.Where(x => x.Indemnity != null || x.IndemnityPL != null || x.LimitOfIndemnity_Currency != null).ToList();
    t2a.Quote_SubLimitOfIndemnitys = t2a.Quote_SubLimitOfIndemnitys.Where(x => x.Indemnity != null || x.IndemnityPL != null || x.SubLimitOfIndemnity_Currency != null).ToList();
    obj.TabCover2a = t2a;

    Session.Add("CASQuote", obj);
    ViewBag.Quote_Status_ID = JsonConvert.SerializeObject(obj.Quote_Status_ID);
}

The test method I've attempted so far is:

[TestMethod]
public void CASSaveCover2AData()
{
    builder.InitializeController(controller);
    var CASTabCover2a = new CASTabCover2a();
    controller.SaveCover2AData(CASTabCover2a);
    Assert.IsTrue(true);
}

When I am debugging this test case I am getting this error:

An exception of type 'System.NullReferenceException' occurred

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
  • Apart from what @DaveyDaveDave had said have a look at [Unit testing Void Methods](https://stackoverflow.com/a/246060/2417602). – vikscool May 14 '19 at 07:24
  • Thank you. In general, please remember to edit your questions, rather than adding content in comments. Comments get deleted, and, as you can see above, code formatting doesn't work. I've made the edit for you here. – DaveyDaveDave May 14 '19 at 07:49
  • I don't really know C#, but my guess would be that the problem is that you're not setting any of the properties in the `CASTabCover2a` object that you create in your tests, but then you're trying to refer to several properties in the `SaveCover2AData` method, which are `null`. – DaveyDaveDave May 14 '19 at 07:52
  • you should refactor your method instead of trying to unit test it – Hien Nguyen May 14 '19 at 08:06
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Marco May 14 '19 at 08:24

1 Answers1

0

First, your variable name is invalid here

 var CASTabCover2a = new CASTabCover2a();

Second, void methods affects the database, inserts data or deletes data. In this point before run your void method get the counts of total rows in related table. If row count changed, your method works correctly.

If you insert data, total counts of row must be increased. If deleted vice versa.

public void MyTestMethod()
{
    var total = db.Cars.Count(); 
    var car = new Car(); 
    db.Save(car);
    var total2 = db.Cars.Count();
    Assert.True(total2 > total);
}
Onur
  • 1
  • 1