我有以下测试代码:
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或空的枚举(即,当我使用调试器检查返回的值时,“Results View”表示该枚举未返回结果)。
我在这里想念什么?
最佳答案
milestones.Count()
像这样执行(因为这是IEnumerable对象):
因此,我建议您进行一些重写。
选项1:
List
或Array
:var milestones = viewModel.Milestones.ToArray();
//var milestones = viewModel.Milestones.ToList();
之后,您可以分别使用
Count
和Length
属性进行Assert
检查:Assert.That(milestones.Count, Is.EqualTo(1));
//Assert.That(milestones.Length, Is.EqualTo(1));
var count = viewModel.Milestones.Count(); // .Count() method executes here.Assert.That(count, Is.EqualTo(1));
关于.net - 犀牛Mo头: Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6279989/