1

I want to stub ModelState.IsValid, so I'm using following construction:

SomeController controller = MockRepository.GenerateStub<SomeController>();
controller.Stub(x => x.ModelState.IsValid).Return(false);

After invoke, I get:

System.InvalidOperationException: Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

Why this exception is thrown and how the ModelState.IsValid can be stubbed ? Do I have to do something like this: controller.ModelState.AddModelError("", "") for invalidating model state ?

Regards

jwaliszko
  • 16,942
  • 22
  • 92
  • 158

1 Answers1

3

For testing validation, I usually pass the action a model which fails validation, similar to Scott Hanselman's answer here.

If that's not an option then use controller.ModelState.AddModelError("", "").

Community
  • 1
  • 1
Lester
  • 4,243
  • 2
  • 27
  • 31