0

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.

This is my method it takes in a PlayerID, BadgeID, and GameID.

 public bool Badge(int pID, int bID, int gID = 0)
    {
        var list = EliminationDbContext.PlayerBadges.Where(x => x.Badge.BadgeID.Equals(bID) && 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;
    }

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

        [Test]
        public void testing()
        {    //Arrange
            var mock = new Mock<IRepoManager>();
            var mockRequest = new Mock<PlayerBadge>();

            var dManager = new TestMoq(mock.Object);
            //set mockRequest to playerBadge
            mockRequest.Setup(x => 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() >= 1);
}
hmad
  • 33
  • 6
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – David Apr 25 '18 at 17:57
  • `The main issue is my understanding of moq.` Reference [Moq Quickstart](https://github.com/Moq/moq4/wiki/Quickstart) – Nkosi Apr 25 '18 at 18:34
  • Unrelated tip: `Assert.That(dManager._RepoManager.PlayerBadges.GetPlayerBadges().Count() >= 1);` is better written as `Assert.That(dManager._RepoManager.PlayerBadges.GetPlayerBadges().Count(), Is.GreaterThanOrEqualTo(1));`. If the original assertion fails, it just says "expected true but was false"; the replacement gives better information. – Richardissimo Apr 28 '18 at 05:37
  • It's not clear from the question what `TestMoq` is. It's quite a jump, but I'm guessing the parameter given to its constructor is exposed through the `_RepoManager` property. And since you haven't set up how PlayerBadges should behave it will return null. The existing Setup line is redundant. Try `mock.SetupProperty(x => x.PlayerBadges, new List<*whatever*>());`, which initializes that property in the mock to have that value. – Richardissimo Apr 28 '18 at 05:50
  • Unrelated tip: rather than Linq `Where`, and the only use is Linq `Any`, replace the `Where` with `Any`, which turns your `var` into a boolean rather than a list. – Richardissimo Apr 28 '18 at 05:55
  • I've just workout out that my SetupProperty suggestion may not work, as I may have incorrectly guessed that PlayerBadges was a collection of things. (See how hard it is for someone to answer this question?) I'm now thinking it might be badly named and perhaps refer to a PlayerBadgesRepository? – Richardissimo Apr 28 '18 at 06:00
  • It may help your understanding to name your mock variables better. For example rather than `var mockRequest = new Mock();` rename that variable to be `mockPlayerBadge` and rather than `var mock = new Mock();` rename it as `mockRepoManager`. – Richardissimo Apr 28 '18 at 06:12

0 Answers0