Ahojte
Ludia, ako ste riesili UnitTesty? Narazil som tu na taky problem...
Ide o to, ze v triede Field mame definovane eventy, konkretne mi ide o DisplayPlaying.
V metode void MoveTile(Tile tile) pri GameState.PLAYING "spustam" tento event.
Problem je v tom, ze nema priradene ziadne delegaty (tie sa priraduju v konstruktore ConsoleUI).
Teda mi test pada na null pointer exception v mieste volania tohto eventu v metode MoveTile.
No a v triede UnitTestField mi to nechce dovolit vytvorit objekt ConsoleUI.
V com je problem?
Tymto to chcem testovat:
[TestMethod]
public void TestMethod1()
{
Field field = new Field(4, 4);
field.State = GameState.PLAYING;
field.MoveTile(3, 2);
field.MoveTile(3, 3);
Assert.AreEqual(GameState.SOLVED, field.State);
}
Metoda MoveTile(Tile tile):
public void MoveTile(Tile tile)
{
int x1 = tile.Col, x2 = greyTile.Col, y1 = tile.Row, y2 = greyTile.Row;
if ((Math.Abs(x1 - x2) == 1) && (Math.Abs(y1 - y2) == 0)
||
(Math.Abs(x1 - x2) == 0) && (Math.Abs(y1 - y2) == 1))
{
GreyTile backupGreyTile = greyTile;
greyTile.Row = y1;
greyTile.Col = x1;
tile.Row = y2;
tile.Col = x2;
Tiles[greyTile.Row, greyTile.Col] = backupGreyTile;
Tiles[tile.Row, tile.Col] = tile;
if (State != GameState.GENERATION)
{
DisplayPlaying();
}
else
{
DisplayGeneration();
}
if (IsSolved())
{
State = GameState.SOLVED;
}
}
else
{
throw new MoveTileException("Can not move this tile");
}
}
Problem vznika tu:
if (State != GameState.GENERATION)
{
DisplayPlaying();
}
No a DisplayPlaying ma priradene delegat tu:
public ConsoleUI(Field field)
{
this.field = field;
field.DisplayPlaying += UpdateUI;
field.DisplayGeneration += UpdateUI;
field.DisplayGeneration += Sleep;
}
Budem vdacny za akukolvek radu.
Ak je to banalita, tak prepacte, ale neviem si dat rady.
EDIT:
Uz to mam, to je uzasne, ako clovek pride na riesenie, ked to takto pekne niekam napise a to po sebe precita
.
Staci testovat na NULL a iba v pripade, ze Event ma priradene delegaty, tak ju volat...