tags:

views:

235

answers:

1

Can someone tell me whey the following code blows when the moq.SetupProperty fails in the code below:

[TestMethod]
public void SimulatorService_Returns_HighScores()
{
    IScoreService scoreService = new ScoreService(MockScoreRepository.GetMockScoreRepository());
    Assert.IsNotNull(scoreService);
    var highScores = scoreService.GetHighScores();
    Assert.IsTrue(highScores.Count > 0);
}


public static class MockScoreRepository
{

    public static ScoreEntry GetMockScoreEntry(int seed)
    {
        var moq = new Mock<ScoreEntry>();

        moq.SetupProperty(s => s.UserID, seed);
        moq.SetupProperty(s => s.Score, 10 * seed);
        moq.SetupProperty(s => s.EntryDate, DateTime.Now);

        return moq.Object;
    }

    public static IScoreRepository GetMockScoreRepository()
    {
        var scores = new List<ScoreEntry>();
        for (var i = 0; i < 20; i++)
        {
            scores.Add(GetMockScoreEntry(i));
        }

        var repository = new Mock<IScoreRepository>();
        repository.Setup(r => r.GetScores()).Returns(scores.AsQueryable());

        return repository.Object;
    }
}
A: 

The first thing I'd check is to make sure that the properties of ScoreEntry that you're setting have accessible setters. In other words, make sure your setters have public access or that you at least have a setter for each property you're attempting to set through moq.

Sergey