Reputation: 192
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?
Upvotes: 0
Views: 235
Reputation: 260
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);
}
}
}
Upvotes: 1
Reputation: 9769
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.
Upvotes: 1
Reputation: 43
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)
Upvotes: 0