TDD Data Loading Methods

I am TDD newb, and I would like to figure out how to test the following code.

At first I try to write my tests, but I had problems creating a test that concerns my DataAccessor. I can’t figure out how to do this. I increased the dispatch class and overriding the Load () method; continue testing the facility. I feel as if in the end I am testing my Mock objects / stubs, not my real objects. I thought that in TDD, unit tests should have hit ALL of the methods on the object; however, I can never verify that the Load () code only overrides the Mock Load

My tests were written by an object that contains a list of orders based on shipping.

I have an object that is loading from a database.

public class Shipment { //member variables protected List<string> _listOfOrders = new List<string>(); protected string _id = "" //public properties public List<string> ListOrders { get{ return _listOfOrders; } } public Shipment(string id) { _id = id; Load(); } //PROBLEM METHOD // whenever I write code that needs this Shipment object, this method tries // to hit the DB and fubars my tests // the only way to get around is to have all my tests run on a fake Shipment object. protected void Load() { _listOfOrders = DataAccessor.GetOrders(_id); } } 

I create my fake dispatch class to test the rest of the class methods. I can never test the Real load method without actually connecting to the DB

 public class FakeShipment : Shipment { protected new void Load() { _listOfOrders = new List<string>(); } } 

Any thoughts? Please inform.

Dave

+4
source share
2 answers

I assume DataAccessor is currently a static class.
The first step would be to create an abstraction for the DataAccessor by creating an interface.

IDataAccessor

Then you have two options: make IDataAccessor constructor dependencies see below:

 public class Shipment { private readonly IDataAccessor dataAccessor; public Shipment(IDataAccessor dataAccessor) { this.dataAccessor = dataAccessor; } } 

Or use the Double Dispatch method shown below:

  public void Load(IDataAccessor dataAccessor) { _listOfOrders = dataAccessor.GetOrders(_id); } 

Then, in your device tests, you will use the stub implementation for IDataAccessor.

+3
source

do it

  public Shipment(DataAccessor da, string id) { _da = da; _id = id; Load(); } protected void Load() { _listOfOrders = _da.GetOrders(_id); } 

DataAccessor should probably be an interface.

btw, these protected data members smell like rotten fish.

+1
source

Source: https://habr.com/ru/post/1303425/


All Articles