{"__v":6,"_id":"5447ed4d8d7af31a00dd411d","category":{"__v":5,"_id":"5447ed118d7af31a00dd411c","pages":["54485515c1b42e08005b82bd","5447ed4d8d7af31a00dd411d","5447ef460319802200fc070d","54485571c1b42e08005b82c1","54af53eb0cf42a0b001d5b73"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-22T17:44:49.648Z","from_sync":false,"order":3,"slug":"assertions","title":"Assertions"},"is_link":false,"project":"54471fc9e12a270800028adc","user":"54471f91beb6320800da6f75","version":{"__v":10,"_id":"54471fc9e12a270800028adf","project":"54471fc9e12a270800028adc","createdAt":"2014-10-22T03:08:57.750Z","releaseDate":"2014-10-22T03:08:57.750Z","categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":["545e5478689e0f16007a5948","545ea2b3689e0f16007a59bb"],"next":{"pages":[],"description":""},"createdAt":"2014-10-22T17:45:49.554Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"basic_auth":false,"results":{"codes":[]},"try":true,"auth":"never","params":[],"url":""},"isReference":false,"order":1,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ShouldLookLike\"\n}\n[/block]\nThe [Should library](http://specsfor.readme.io/v1.0/docs/the-should-library) has numerous extension methods for assertions.  But what if you wanted to compare the properties of two objects?  You might write a test like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_returns_the_expected_model()\\n{\\n     _account.ShouldEqual(new AccountViewModel\\n     {\\n          AccountNumber = 12345,\\n          CustomerName = \\\"Atticus Finch\\\",\\n          Balance = 5042.61\\n     });\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThis test will fail because .ShouldEqual() will perform a comparison on two separate instances of the same class.  What we really want to happen is to compare the properties of the two instances of the AccountViewModel class to make sure they are all the same.  SpecsFor includes the ShouldLookLike() extension method for this purpose.  The ShouldLookLike() extension method uses the power of the [ExpectedObjects](https://github.com/derekgreer/expectedObjects) library to compare two complex objects.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_it_returns_the_expected_model()\\n{\\n     _account.ShouldLookLike(new AccountViewModel\\n     {\\n          AccountNumber = 12345,\\n          CustomerName = \\\"Atticus Finch\\\",\\n          Balance = 5042.61\\n     });\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThis test will pass because ShouldLookLike() will compare each property of the model to check that the value is the same between the two instantiations.\n\nBut what if one of the properties of _account has a different value than what we expect?  The test will fail, but especially with a much larger model, it could be difficult to tell which properties are not equal.\n\nWhen the test fails, ShouldLookLike() will list the properties that are not equal so we quickly know where the problem is.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"System.Exception : For AccountViewModel.Balance, expected [5042.61] but found [4872.57].\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ShouldContain and ShouldNotContain\"\n}\n[/block]\nSometimes you want to know if an enumerable contains an item matching some arbitrary criteria.  For example, you may want to make sure that a collection of users contains at least one user with the name \"John Doe.\"   You could check for this using a combination of LINQ and Should, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_john_doe_was_added()\\n{\\n     _addedUsers.Any(u => u.UserName == \\\"John Doe\\\").ShouldBeTrue();\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nHowever, if your spec fails, you won't get a very useful error message.  You'll get a generic \"Expected true, got false\" error.  It would be more useful if the failure message told you what it failed to find in the collection.  \n\nThe ShouldContain extension method handles this same scenario, and it gives you a much better message if the expected item isn't found:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_john_doe_was_added()\\n{\\n    //Will output \\\"Expected list containing an item matching 'u => u.UserName == \\\"John Doe\\\"', but item was not found.\\n\\t\\t_addedUsers.ShouldContain(u => u.UserName == \\\"John Doe\\\");\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou may also want to make sure that a particular item is not in a list.  For example, what if we wanted to make sure Jane Doe was not in our list of users? \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_jane_doe_was_not_added()\\n{\\n    //Will output \\\"Expected list not containing an item matching 'u => u.UserName == \\\"Jane Doe\\\"', but item was found.\\n\\t\\t_addedUsers.ShouldNotContain(u => u.UserName == \\\"Jane Doe\\\");\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ShouldContainAll\"\n}\n[/block]\nSometimes your specs will be verifying the contents of strings that are generated by your system under test, such as checking a log message, checking the contents of an E-mail, etc.  If your test is *too *specific, it could fail when the message is reworded slightly.  Usually, it's enough just to make sure that certain key pieces of information, such as the correct user name or date, are present in the string.  With ShouldContainAll, you can easily check that a string contains one or more tokens:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_the_message_includes_the_date_and_the_users_name()\\n{\\n    _passwordResetText.ShouldContainAll(\\\"John Doe\\\", \\\"11/10/2014\\\");\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"additional-should-assertions","type":"basic","title":"Additional \"Should\" assertions"}

Additional "Should" assertions


[block:api-header] { "type": "basic", "title": "ShouldLookLike" } [/block] The [Should library](http://specsfor.readme.io/v1.0/docs/the-should-library) has numerous extension methods for assertions. But what if you wanted to compare the properties of two objects? You might write a test like this: [block:code] { "codes": [ { "code": "[Test]\npublic void then_returns_the_expected_model()\n{\n _account.ShouldEqual(new AccountViewModel\n {\n AccountNumber = 12345,\n CustomerName = \"Atticus Finch\",\n Balance = 5042.61\n });\n}", "language": "csharp" } ] } [/block] This test will fail because .ShouldEqual() will perform a comparison on two separate instances of the same class. What we really want to happen is to compare the properties of the two instances of the AccountViewModel class to make sure they are all the same. SpecsFor includes the ShouldLookLike() extension method for this purpose. The ShouldLookLike() extension method uses the power of the [ExpectedObjects](https://github.com/derekgreer/expectedObjects) library to compare two complex objects. [block:code] { "codes": [ { "code": "[Test]\npublic void then_it_returns_the_expected_model()\n{\n _account.ShouldLookLike(new AccountViewModel\n {\n AccountNumber = 12345,\n CustomerName = \"Atticus Finch\",\n Balance = 5042.61\n });\n}", "language": "csharp" } ] } [/block] This test will pass because ShouldLookLike() will compare each property of the model to check that the value is the same between the two instantiations. But what if one of the properties of _account has a different value than what we expect? The test will fail, but especially with a much larger model, it could be difficult to tell which properties are not equal. When the test fails, ShouldLookLike() will list the properties that are not equal so we quickly know where the problem is. [block:code] { "codes": [ { "code": "System.Exception : For AccountViewModel.Balance, expected [5042.61] but found [4872.57].", "language": "text" } ] } [/block] [block:api-header] { "type": "basic", "title": "ShouldContain and ShouldNotContain" } [/block] Sometimes you want to know if an enumerable contains an item matching some arbitrary criteria. For example, you may want to make sure that a collection of users contains at least one user with the name "John Doe." You could check for this using a combination of LINQ and Should, like so: [block:code] { "codes": [ { "code": "[Test]\npublic void then_john_doe_was_added()\n{\n _addedUsers.Any(u => u.UserName == \"John Doe\").ShouldBeTrue();\n}", "language": "csharp" } ] } [/block] However, if your spec fails, you won't get a very useful error message. You'll get a generic "Expected true, got false" error. It would be more useful if the failure message told you what it failed to find in the collection. The ShouldContain extension method handles this same scenario, and it gives you a much better message if the expected item isn't found: [block:code] { "codes": [ { "code": "[Test]\npublic void then_john_doe_was_added()\n{\n //Will output \"Expected list containing an item matching 'u => u.UserName == \"John Doe\"', but item was not found.\n\t\t_addedUsers.ShouldContain(u => u.UserName == \"John Doe\");\n}", "language": "csharp" } ] } [/block] You may also want to make sure that a particular item is not in a list. For example, what if we wanted to make sure Jane Doe was not in our list of users? [block:code] { "codes": [ { "code": "[Test]\npublic void then_jane_doe_was_not_added()\n{\n //Will output \"Expected list not containing an item matching 'u => u.UserName == \"Jane Doe\"', but item was found.\n\t\t_addedUsers.ShouldNotContain(u => u.UserName == \"Jane Doe\");\n}", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "ShouldContainAll" } [/block] Sometimes your specs will be verifying the contents of strings that are generated by your system under test, such as checking a log message, checking the contents of an E-mail, etc. If your test is *too *specific, it could fail when the message is reworded slightly. Usually, it's enough just to make sure that certain key pieces of information, such as the correct user name or date, are present in the string. With ShouldContainAll, you can easily check that a string contains one or more tokens: [block:code] { "codes": [ { "code": "[Test]\npublic void then_the_message_includes_the_date_and_the_users_name()\n{\n _passwordResetText.ShouldContainAll(\"John Doe\", \"11/10/2014\");\n}", "language": "csharp" } ] } [/block]