Ross
Ross

Reputation: 325

Unit test failure System.ArgumentNullException

I am writing a unit test for a project I am working on and every time I run the second test method CheckIfRoomTwoHasTheCorrectProperties() I get a System.ArgumentNullException.

[TestMethod]
public void CheckIfRoomTwoHasTheCorrectProperties()
{

    IDungeonController objectUndertest = new DungeonController();

    IRoom secondRoom = objectUndertest.Rooms.FirstOrDefault(y => y.RoomNumber == 2);
    Assert.IsNotNull(secondRoom, "Should create room number 2");
    Assert.AreEqual(2, secondRoom.Doors.Count);

    Assert.AreEqual(1, secondRoom.RoomContents.Count);
    Assert.AreEqual("Key", secondRoom.RoomContents[1].Name, "Unexpected object added to room");

    IDungeonDoor southWallUnlocked = secondRoom.Doors.FirstOrDefault(
                                                     x => x.Location == DoorLocationEnum.South 
                                                     && x.Door.IsLocked == false);
    Assert.IsNotNull(southWallUnlocked, "Door in south wall is unlocked");

    IDungeonDoor eastWallUnlocked = secondRoom.Doors.FirstOrDefault(
                                                    x => x.Location == DoorLocationEnum.East 
                                                    && x.Door.IsLocked == false);
    Assert.IsNotNull(eastWallUnlocked, "Door in south wall is locked");
}

The exception is thrown in the above code here;

objectUndertest.Rooms.FirstOrDefault(y => y.RoomNumber == 2);
Assert.IsNotNull(secondRoom, "Should create room number 2");

The test for room one below works fine

[TestMethod]
public void CheckIfRoomOneHasTheCorrectProperties()
{
    IDungeonController ojectUnderTest = new DungeonController();

    ojectUnderTest.Initialise();

    IRoom firstRoom = ojectUnderTest.Rooms.FirstOrDefault(y => y.RoomNumber == 1);
    Assert.IsNotNull(firstRoom, "Should create room number 1");
    Assert.AreEqual(3, firstRoom.Doors.Count);

    Assert.AreEqual(2, firstRoom.RoomContents.Count);
    Assert.AreEqual("Tap", firstRoom.RoomContents[0].Name, "Unexpected object added to room");

    IDungeonDoor southWallUnlocked = firstRoom.Doors.FirstOrDefault(
                                                     x => x.Door.IsLocked == false 
                                                     && x.Location == DoorLocationEnum.South);
    Assert.IsNotNull(southWallUnlocked, "Door in south wall should be unlocked");

    IDungeonDoor northWallUnlocked = firstRoom.Doors.FirstOrDefault(x => 
                                                    x.Door.IsLocked == false 
                                                    && x.Location == DoorLocationEnum.North);
    Assert.IsNotNull(northWallUnlocked, "Door in north wall should be unlocked");

    IDungeonDoor eastWallLoacked = firstRoom.Doors.FirstOrDefault(
                                                   x => x.Door.IsLocked == true 
                                                   && x.Location == DoorLocationEnum.East);
    Assert.IsNotNull(eastWallLoacked, "Door in east wall should be locked");           
}

The code for both test is

IRoom room1 = new Room();
room1.RoomNumber = 1;
room1.AddDungeonDoor(new DungeonDoor(new Door(), DoorLocationEnum.South));
room1.AddDungeonDoor(new DungeonDoor(new Door(), DoorLocationEnum.North));
room1.AddDungeonDoor(new DungeonDoor(new Door{IsLocked = true}, DoorLocationEnum.East));
room1.AddRoomObject(new Tap());        
Rooms.Add(room1);

IRoom room2 = new Room();
room2.RoomNumber = 2;
room2.AddDungeonDoor(new DungeonDoor(new Door {IsLocked = false }, DoorLocationEnum.South));
room2.AddDungeonDoor(new DungeonDoor(new Door { IsLocked = false}, DoorLocationEnum.East));
room1.AddRoomObject(new Key());
Rooms.Add(room2);

Upvotes: 0

Views: 94

Answers (1)

forsvarir
forsvarir

Reputation: 10839

As @Reg Edit suggested in his comment, the test CheckIfRoomTwoHasTheCorrectProperties was missing a call to ojectUnderTest.Initialise(); during it's Arrange step.

Upvotes: 1

Related Questions