c# - How to unit test private properties? -


i'm pretty new tdd , have hard time understand how test private members of class (i know! it's private, shouldn't tested - please keep reading). might have public function sets private property , other public function returns "something" based on private property.

let me show basic example:

public class cell {     public int x { get; set; }     public int y { get; set; }     public string value { get; set; } }  public class table {     private cell[,] cells { get; }     public table(cell[,] cells)     {         cells = cells;     }      public void setcell(int x, int y, string value)     {         cells[x, y].value = value;     }      public void reset()     {         (int = 0; < cells.getlength(0); i++)         {             (int j = 0; j < cells.getlength(1); j++)             {                 cells[i, j].value = "";             }         }     }      public bool areneighborcellsset(int x, int y)     {         bool areneighborcellsset = false;          // checking...          return areneighborcellsset;     } } 

in example cells private, because there's no reason make them public. don't need know what's value of particular cell outside class. need information if neighbor cells empty.

1. how can test reset method?

technically should create table mocked array of cells. call reset , assert if every cell has empty value. can't check if empty or not.

2. in case call assert many times (for every cell) - practice? i've read "it's not!", reset resets cells, have somehow check every cell.

edit: option 2:

public class table {     private cell[,] cells { get; }      public table(int height, int width, icellfactory cellfactory)     {         cells = new icell[height, width];         (int = 0; < cells.getlength(0); i++)         {             (int j = 0; j < cells.getlength(1); j++)             {                 cells[i, j].value = cellfactory.create(i, j);             }         }     }      // rest same... } 

your class have 3 public methods

void setcell void reset bool areneighborcellsset 

so functionality should tested through methods , possible of constructor input arguments.

i afraid not doing tdd, because trying test implemented logic (for loop of internal member). tdd should write unit tests using public api of class under test.

when testing reset method should think - how affect on results of other public methods. table class have 1 method return value can observe - bool areneighborcellsset - seems method against can execute our asserts.

for reset method need set cells areneighborcellsset returns true. execute reset , assert areneighborcellsset returns false.

[test] public void afterresetgivencellshouldnothaveneighbors() {     // arrange     var cell = new cell { x = 1, y = 1, value = "central" };     var neighborcell = new new cell { x = 1, y = 2, value = "neighbor" };     var table = new table(new[] { cell, neighborcell });      // table.areneighborcellsset(cell.x, cell.y) - should return true @ moment     // act     table.reset();      // assert     table.areneighborcellsset(cell.x, cell.y).should().befalse(); } 

this example of tdd (test-driven development), problems testing - sign, wrong design.

actually, think, in case don't need reset method @ - create new instance of table every time need reset it.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -