{"__v":2,"_id":"54cfcc24bba1a02300874200","category":{"__v":11,"_id":"54cfc6f75ff7e617002bbdae","pages":["54cfc714bba1a023008741f1","54cfcc24bba1a02300874200","54cfcc545ff7e617002bbdbe","54cfccf73995cf0d0006f729","54cfce14bba1a02300874205","54cfce42bba1a02300874209","54cfceef3995cf0d0006f736","54cfcf7dbba1a02300874218","54cfcfae3995cf0d0006f73b","54cfcfc4bba1a0230087421b","54cfcfdc3995cf0d0006f73d"],"project":"54cfb736bba1a0230087419e","version":"54cfb736bba1a023008741a1","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-02T18:50:31.446Z","from_sync":false,"order":2,"slug":"usage","title":"Usage"},"project":"54cfb736bba1a0230087419e","user":"54c4ca54686bce0d00efe1b9","version":{"__v":5,"_id":"54cfb736bba1a023008741a1","project":"54cfb736bba1a0230087419e","createdAt":"2015-02-02T17:43:18.560Z","releaseDate":"2015-02-02T17:43:18.560Z","categories":["54cfb737bba1a023008741a2","54cfc6f75ff7e617002bbdae","54cfcbaebba1a023008741fc","54cfd0295ff7e617002bbdd9","54cfd05b3995cf0d0006f741"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-02-02T19:12:36.566Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"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](http://robdmoore.id.au/blog/2012/04/27/unobtrusive-validation-in-asp-net-mvc-3-and-4/)), sometimes it's useful.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Check that there are no model errors\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>().WithNoModelErrors();\\n\\n// Check that there is a model error against a given property in the model\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>().AndModelErrorFor(m => m.Property1);\\n\\n// Check that there isn't a model error against a given property in the model\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>().AndNoModelErrorFor(m => m.Property1);\\n\\n// Check that there is a model error against a specific key\\n// Avoid if possible given it includes a magic string\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>().AndModelError(\\\"Key\\\");\\n\\n// You can chain these model error calls and thus check for multiple errors\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1)\\n    .AndModelErrorFor(m => m.Property2);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou 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:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Equality\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1).ThatEquals(\\\"The error message.\\\");\\n\\n// Start of message\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1).BeginningWith(\\\"The error\\\");\\n\\n// End of message\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1).EndingWith(\\\"message.\\\");\\n\\n// Containing\\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1).Containing(\\\"e error m\\\");\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can chain the error property checks after any of these checks (you can only perform one of the checks though):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\\n    .WithModel<ModelType>()\\n    .AndModelErrorFor(m => m.Property1).ThatEquals(\\\"The error message.\\\")\\n    .AndModelErrorFor(m => m.Property2);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"model-binding-errors","type":"basic","title":"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](http://robdmoore.id.au/blog/2012/04/27/unobtrusive-validation-in-asp-net-mvc-3-and-4/)), sometimes it's useful. [block:code] { "codes": [ { "code": "// Check that there are no model errors\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>().WithNoModelErrors();\n\n// Check that there is a model error against a given property in the model\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>().AndModelErrorFor(m => m.Property1);\n\n// Check that there isn't a model error against a given property in the model\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>().AndNoModelErrorFor(m => m.Property1);\n\n// Check that there is a model error against a specific key\n// Avoid if possible given it includes a magic string\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>().AndModelError(\"Key\");\n\n// You can chain these model error calls and thus check for multiple errors\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1)\n .AndModelErrorFor(m => m.Property2);", "language": "csharp" } ] } [/block] 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: [block:code] { "codes": [ { "code": "// Equality\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1).ThatEquals(\"The error message.\");\n\n// Start of message\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1).BeginningWith(\"The error\");\n\n// End of message\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1).EndingWith(\"message.\");\n\n// Containing\n_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1).Containing(\"e error m\");", "language": "csharp" } ] } [/block] You can chain the error property checks after any of these checks (you can only perform one of the checks though): [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()\n .WithModel<ModelType>()\n .AndModelErrorFor(m => m.Property1).ThatEquals(\"The error message.\")\n .AndModelErrorFor(m => m.Property2);", "language": "csharp" } ] } [/block]