mardi 6 septembre 2016

Do I need to unit test this function?

I've been researching a lot about unit testing and I have a couple functions whose only purpose is to add or update rows in a database based on user input from the model. From what I've read, it doesn't seem like unit testing is really used for that. An integration test might make more sense but I haven't researched those as much yet. I'm sorry if this is covered elsewhere, I tried to find a similar example and this questions is the closest I could find: http://ift.tt/2cDFi9a ...which implies that a unit test is not the best way to test a function that just calls the database.

Here's an example of one of our db functions...

    public void AddDependant(AddDependantView addDependantView, int ID)
    {
        using (var db = new EnrollmentDataModel())
        {
            Dependant dependant = new dependant();
            dependant.ID = ID;
            dependant.FirstName = addDependantView.FirstName;
            dependant.LastName = addDependantView.LastName;
            dependant.Relation = addDependantView.Relation;
            dependant.Birthday = (addDependantView.BirthDate).Value.ToString("MM/dd/yyyy");
            dependant.Active = true;
            dependant.RowCreatedDateTime = DateTime.Now;

            db.Dependant.Add(dependant);
            db.SaveChanges();

            UpdateLog(Dependant.ID, "Dependant Data Added", "User added their dependant data.");
        }
    }

The "UpdateLog" function called at the end is similar in structure to this one but adds an entry in a different database that logs any user activity.

So would this function benefit from unit testing? If it does what would be the benefit? Would an integration test be a better alternative? I'm sorry if this is too basic or too broad but I'm pretty new to unit testing and want to get into good practices.

Aucun commentaire:

Enregistrer un commentaire