0

I have a function like,

public void UpdateData()
{
  Data data = GetDataFromDatabase()

  if(!data.IsDefault)
  {  
   // Do some change in data object
     SaveData(data);
  }
}

I want write unit test for SaveData(Data data) function,

public bool SaveData(Data data)
{
  // Code for saving data in database
}

Above function contains code for saving data in database and there are no validations. Validations are done before calling this function.

Applications requirement is default data should not be updated. So while writing unit test should I consider this requirement?

If I write Unit Test like,

public void SaveData_DefaultData()
{
   SaveData(defaultData);
   Assert.IsTrue(); // Some condition in assert to check whether the default data is updated.
}

By looking at the SaveData function I know that this test case is going to fail. So my question is do I need to consider this test case?

3
  • What is default data here? Can you share the code of SaveData method? What is the logic in the method which does not update the default data? Commented Jul 25, 2018 at 3:47
  • @ChetanRanpariya 'Data defaultData' ( where defaultData.IsDefault == true ). And SaveData() function just saves/updates data in database nothing else. defaultData is retrieved from database, changed and passed to the function to update. Commented Jul 25, 2018 at 4:37
  • saves/updates data in database nothing else can you share this code? You are trying to unit test the code of SaveData method? Commented Jul 25, 2018 at 4:43

3 Answers 3

1

One of the very important thing with writing unit tests is that the test should be verifying the behavior within the context of the class. In this case, the SaveData method is actually going to the database and making the change there. It is more an integration test than a unit test. There could be a situation where the test might fail because of network or some other issue, but your class is perfectly working as expected.

Ideally, for the scenarios where the UT is going beyond the scope of the class, you should have Dependency Injection in place where your class should be able to except a different implementation for UTs. Then you should use some mocking framework like Moq to provide the expectation during the execution of UTs.

You would like to read some more on the above design here , here, and here.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, Any public class must be tested. To do this, you need to test your object in the following way:

    public interface ITest
   {
      bool SaveData(Data data);
   }

so create a fake object To export the method to a logic that is to send our method to the database

 public class FakeTest : ITest
 {
    public bool SaveData(Data data)
    {
       \\Fake Code Return so Like : return true;
    }
 }

so create MsTest Or NUnit For Test

 namespace MsTest
  {
      [TestClass]
      public Class TestDataBase 
      {
           [TestMethod()]
           public void SendTest()
           {
             Mock<ITest> smtpClient = new Mock<ITest>();
             var DataBase = new DataBase(smtpClient.Object);
             bool save = DataBase.SaveData(New Data());
             Assert.IsTrue(save);
        }
      }
  }

Comments

0

I would assume that if you are referring to default data, that default data should remain in the DB at all times (this includes your Unit test DB) so there should never be a need to SaveData performed with the defaultData. Now if your unit test was to assert that the default data were not updated, I would probably suggest that the unit test trigger something that inserts data into the DB and assert from there that the default data has remained unchanged.

A test case like the one above seems to not test anything. Your save data will insert/update the default data (whatever it does) and unless your Database explicitly prevents that action your save data will success and that would be correct. but not correct in terms of your system.

That would be an appropriate test only if your database prevents the default data from being updated. (in my humble opinion)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.