View Model Errors

Once you have made assertions about the model you can then make assertions that particular model errors are present for properties of that model. While it's not generally the best idea to add validation logic to controllers (doing it unobtrusively is best), sometimes it's useful.

// Check that there are no model errors
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>().WithNoModelErrors();

// Check that there is a model error against a given property in the model
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>().AndModelErrorFor(m => m.Property1);

// Check that there isn't a model error against a given property in the model
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>().AndNoModelErrorFor(m => m.Property1);

// Check that there is a model error against a specific key
// Avoid if possible given it includes a magic string
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>().AndModelError("Key");

// You can chain these model error calls and thus check for multiple errors
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1)
    .AndModelErrorFor(m => m.Property2);

You can also make assertions on the content of the error message(s); these methods will look for any error messages against that particular model state key that match the given criteria:

// Equality
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1).ThatEquals("The error message.");

// Start of message
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1).BeginningWith("The error");

// End of message
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1).EndingWith("message.");

// Containing
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1).Containing("e error m");

You can chain the error property checks after any of these checks (you can only perform one of the checks though):

_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
    .WithModel<ModelType>()
    .AndModelErrorFor(m => m.Property1).ThatEquals("The error message.")
    .AndModelErrorFor(m => m.Property2);