Moq SetupProperty无法正常工作

时间:2009-07-30 03:17:28

标签: moq

有人告诉我,当下面的代码中moq.SetupProperty失败时,下面的代码会爆炸:

[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;
    }
}

1 个答案:

答案 0 :(得分:0)

我要检查的第一件事是确保您设置的ScoreEntry的属性具有可访问的setter。换句话说,请确保您的setter具有公共访问权限,或者至少为您尝试通过moq设置的每个属性设置了setter。