C# Unit Testing – How to Write Tests Without Mocking Data

cunit testing

My design doesn't allow me to mock the data so I am using sqlite as test database that has minimum data to run the unit tests. Below is the pseudo code

//Method to be tested
public IList<Funds> GetFunds()
{    
  List<Funds> objFundsList = //gets two records from sqlite db;
  return objFundsList;
}


//Test Method
public void Check_If_Get_Funds_Returns_List_of_Funds()
{
      FundsService obj = new FundsService();
      var lstFunds =   obj.GetFunds();
      Assert.AreEqual(2,lstFunds.Count());
      //Do I need to get first item here to check if bindings for fund is successful
      var fund = obj.GetFunds().First();
      Assert.AreEqual("test",fund.Name);
}

Since I am not using in-memory mock objects, I can't do sequenceequal. In this scenario, which other tests I can include.

Best Answer

If the order of the result set is undefined by the implementation, then you'll need to write an assertion that's not dependent on the order of the results. CollectionAssert.AreEquivalent is one such method. Here's an example:

// I'm assuming that you've written a helper method to get each fund's name
var actualNames = GetFundNamesFrom(obj);
var expectedNames = ["test1", "test2"];
CollectionAssert.AreEquivalent(expectedNames, actualNames);