I have the following test code:
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();
All the tests and assertions succeed up until the:
Assert.That(milestones.Count(), Is.EqualTo(1));
That's where I get the exception:
Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
I've tried everything I can think of, and my testing seems to indicate that the parentViewModel Mock is returning null, or an empty enumeration (i.e. when I use the debugger to inspect the returned value the 'Results View' says the enumeration returned no results).
What am I missing here?
milestones.Count()
is executing like that (as this is an IEnumerable object):
So I suggest you to get some rewriting.
Option 1:
Create not IEnumerable collection, but some stronger object, like List
or Array
:
var milestones = viewModel.Milestones.ToArray();
//var milestones = viewModel.Milestones.ToList();
After that you can use respectively Count
and Length
property for the Assert
check:
Assert.That(milestones.Count, Is.EqualTo(1));
//Assert.That(milestones.Length, Is.EqualTo(1));
Create a local variable to store count parameter:
var count = viewModel.Milestones.Count(); // .Count() method executes here.
Assert.That(count, Is.EqualTo(1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With