【发布时间】:2011-09-10 22:05:52
【问题描述】:
我有以下测试代码:
parentViewModel = MockRepository.GenerateMock<IParentViewModel>();
parentViewModel.Expect(x => x.GetPropertyValue<IEnumerable<Milestone>>("JobMilestones")).Return(new Milestone[0]);
viewModel = new JobPenaltiesViewModel(j, new Penalty[0], _opContext, parentViewModel);
Assert.That(viewModel.Milestones.Count(), Is.EqualTo(0));
parentViewModel.VerifyAllExpectations();
List<string> propsChanged = new List<string>();
viewModel.PropertyChanged += (s, e) => propsChanged.Add(e.PropertyName);
parentViewModel.Raise(x => x.PropertyChanged += null, parentViewModel, new PropertyChangedEventArgs("JobMilestones"));
AssertPropertiesChangedAsExepected(propsChanged, 1, "Milestones");
Milestone m1 = GenerateMilestone(j);
List<Milestone> milestones1 = new List<Milestone> { m1 };
parentViewModel.Expect(x => x.GetPropertyValue<IEnumerable<Milestone>>("JobMilestones")).Return(milestones1).Repeat.Any();
IEnumerable<Milestone> milestones = viewModel.Milestones;
Assert.That(milestones.Count(), Is.EqualTo(1));
parentViewModel.VerifyAllExpectations();
所有测试和断言都成功,直到:
Assert.That(milestones.Count(), Is.EqualTo(1));
这就是我得到异常的地方:
Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
我已经尝试了我能想到的一切,我的测试似乎表明 parentViewModel Mock 返回 null 或空枚举(即,当我使用调试器检查返回的值时,“结果视图”表示枚举没有返回结果)。
我在这里错过了什么?
【问题讨论】:
-
如果你尝试做:
IEnumerable<Milestone> milestones = parentViewModel.Milestones;和Assert.That(milestones.Count(), Is.EqualTo(1)),你会得到正确的结果吗? (只需验证您松开 m1 的位置) -
不,
Assert.That(milestones.Count(), Is.EqualTo(1))是异常的来源。我从来没有到过parentViewModel.VerifyAllExpectations() -
好吧,我不是很清楚我的意思。我的意思是在
parentViewModel.Expect(x => x.GetPropertyValue<IEnumerable<Milestone>>("JobMilestones")).Return(milestones1).Repeat.Any();之后添加这些行,这样你就可以在parentViewModel上断言,然后再在viewModel上断言。验证m1是否在parentViewModel.JobMileStones中(我在第一条评论中写了 parentViewModel.MileStones - 我的错。)。 -
这给了我:
This action is invalid when the mock object ... is in record state.我不知道为什么它抱怨我处于记录模式,我使用的是 AAA 语法。在尝试返回第二个值之前,是否需要以某种方式重置 Mock? -
我现在实际上已经删除了那段代码;经过进一步审查,我意识到它并没有为我的测试添加任何内容。我真正想在这里测试的是,当父级通知其“JobMilestones”集合已更改时,子级通知其“里程碑”集合已更改。不过,我还是很想了解这个问题,所以如果有人能回答,我很乐意给予答案。
标签: .net unit-testing c#-4.0 rhino-mocks