title: In in moq unit test how to fix "Object reference not set to an instance of an object"
body: <p>Okay I am trying to create a test that checks that my badge method actually added a badge to PlayersBadge. It works but I don't know how to test properly. The main issue is my understanding of moq.</p>
  
  <p>This is my method it takes in a PlayerID, BadgeID, and GameID.</p>
  
  <pre><code> public bool Badge(int pID, int bID, int gID = 0)
      {
          var list = EliminationDbContext.PlayerBadges.Where(x =&gt; x.Badge.BadgeID.Equals(bID) &amp;&amp; x.Player.PlayerID.Equals(pID));
          if (list.Any() != true)
          {
              PlayerBadge b = new PlayerBadge { PlayerID = pID, BadgeID = bID, DateEarned = DateTime.Today, GameID = gID };
              _RepoManager.PlayerBadges.Add(b);
              _RepoManager.SaveDb();
              return true;
          }
          return false;
      }
  </code></pre>
  
  <p>Currently I am getting this error "Object reference not set to an instance of an object" I think it's because I'm not setting up a mock of PlayerBadge correctly but I'm not sure how to fix this</p>
  
  <pre><code>        [Test]
          public void testing()
          {    //Arrange
              var mock = new Mock&lt;IRepoManager&gt;();
              var mockRequest = new Mock&lt;PlayerBadge&gt;();
  
              var dManager = new TestMoq(mock.Object);
              //set mockRequest to playerBadge
              mockRequest.Setup(x =&gt; x.Badge.PlayerBadges.Add(badge));
  
              //Act
              //Object reference not set to an instance of an object on this line 
              dManager._RepoManager.PlayerBadges.Add(mockRequest.Object);
              dManager._RepoManager.Badges.Badge(1, 2, 0);
  
              Assert.That(dManager._RepoManager.PlayerBadges.GetPlayerBadges().Count() &gt;= 1);
  }
  </code></pre>