Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks Error: Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw

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?

like image 354
CodingGorilla Avatar asked Jun 08 '11 14:06

CodingGorilla


1 Answers

milestones.Count() is executing like that (as this is an IEnumerable object):

  1. Set counter to 0.
  2. Get the first element.
  3. Add 1 to the counter.
  4. Move to the next element.
  5. Step 3 until next element is null
  6. Return the counter

So I suggest you to get some rewriting.

Option 1:

  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));

  2. Create a local variable to store count parameter:

    var count = viewModel.Milestones.Count(); // .Count() method executes here. Assert.That(count, Is.EqualTo(1));

like image 55
VMAtm Avatar answered Oct 14 '22 19:10

VMAtm