{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Fuzzy Checks with Some and Any Values","type":"basic","slug":"somevalueof-and-anyvalueof","excerpt":"","body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"NOTE\",\n  \"body\": \"The techniques shown here only work with lambda-based partial matching.  They do not work with anonymous objects.\"\n}\n[/block]\nThe ShouldLookLike assertion also allows you to do fuzzy matching.  For example, you can check to make sure that a property isn't null:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"            [Test]\\n            public void then_you_can_check_for_non_null_values()\\n            {\\n                var engine = new Engine { Maker = \\\"Heroic\\\" };\\n                engine.ShouldLookLike(() => new Engine\\n                {\\n                    Maker = Any.NonNullValueOf<string>()\\n                });\\n            }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can also check for a specific value in a range:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"            [Test]\\n            public void then_you_can_check_for_values_in_a_range()\\n            {\\n                var engine = new Engine {YearBuilt = 2015};\\n                engine.ShouldLookLike(() => new Engine\\n                {\\n                    YearBuilt = Some.ValueInRange(2014, 2016)\\n                });\\n            }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nOr for a value matching some arbitrary criteria:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"            [Test]\\n            public void then_you_can_check_for_a_value_matching_some_expression()\\n            {\\n                var engine = new Engine {Maker = \\\"Heroic\\\"};\\n                engine.ShouldLookLike(() => new Engine\\n                {\\n                    Maker = Some.ValueOf<string>(s => char.IsUpper(s[0]))\\n                });\\n            }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can also check to make sure a list contains some value (or even a *partial* value!)\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"            [Test]\\n            public void then_you_can_check_for_an_item_in_a_list()\\n            {\\n                var warehouse = new Warehouse\\n                {\\n                    Engines = new[] {new Engine {YearBuilt = 2013}, new Engine {YearBuilt = 2016}}\\n                };\\n\\n                warehouse.ShouldLookLike(() => new Warehouse\\n                {\\n                    Engines = Some.ListContaining(() => new Engine\\n                    {\\n                        //Yep, you can use partial matching recursively!\\n                        YearBuilt = Some.ValueOf<int>(i => i % 2 == 0)\\n                    })\\n                });\\n            }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","updates":[],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"54af53eb0cf42a0b001d5b73","category":{"sync":{"isSync":false,"url":""},"pages":["54485515c1b42e08005b82bd","5447ed4d8d7af31a00dd411d","5447ef460319802200fc070d","54485571c1b42e08005b82c1","54af53eb0cf42a0b001d5b73"],"title":"Assertions","slug":"assertions","order":3,"from_sync":false,"reference":false,"_id":"5447ed118d7af31a00dd411c","__v":5,"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","createdAt":"2014-10-22T17:44:49.648Z"},"githubsync":"","__v":2,"version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"_id":"54471fc9e12a270800028adf","__v":10,"project":"54471fc9e12a270800028adc","releaseDate":"2014-10-22T03:08:57.750Z","createdAt":"2014-10-22T03:08:57.750Z"},"project":"54471fc9e12a270800028adc","createdAt":"2015-01-09T04:07:07.740Z","user":"54471f91beb6320800da6f75"}

Fuzzy Checks with Some and Any Values


[block:callout] { "type": "info", "title": "NOTE", "body": "The techniques shown here only work with lambda-based partial matching. They do not work with anonymous objects." } [/block] The ShouldLookLike assertion also allows you to do fuzzy matching. For example, you can check to make sure that a property isn't null: [block:code] { "codes": [ { "code": " [Test]\n public void then_you_can_check_for_non_null_values()\n {\n var engine = new Engine { Maker = \"Heroic\" };\n engine.ShouldLookLike(() => new Engine\n {\n Maker = Any.NonNullValueOf<string>()\n });\n }", "language": "csharp" } ] } [/block] You can also check for a specific value in a range: [block:code] { "codes": [ { "code": " [Test]\n public void then_you_can_check_for_values_in_a_range()\n {\n var engine = new Engine {YearBuilt = 2015};\n engine.ShouldLookLike(() => new Engine\n {\n YearBuilt = Some.ValueInRange(2014, 2016)\n });\n }", "language": "csharp" } ] } [/block] Or for a value matching some arbitrary criteria: [block:code] { "codes": [ { "code": " [Test]\n public void then_you_can_check_for_a_value_matching_some_expression()\n {\n var engine = new Engine {Maker = \"Heroic\"};\n engine.ShouldLookLike(() => new Engine\n {\n Maker = Some.ValueOf<string>(s => char.IsUpper(s[0]))\n });\n }", "language": "csharp" } ] } [/block] You can also check to make sure a list contains some value (or even a *partial* value!) [block:code] { "codes": [ { "code": " [Test]\n public void then_you_can_check_for_an_item_in_a_list()\n {\n var warehouse = new Warehouse\n {\n Engines = new[] {new Engine {YearBuilt = 2013}, new Engine {YearBuilt = 2016}}\n };\n\n warehouse.ShouldLookLike(() => new Warehouse\n {\n Engines = Some.ListContaining(() => new Engine\n {\n //Yep, you can use partial matching recursively!\n YearBuilt = Some.ValueOf<int>(i => i % 2 == 0)\n })\n });\n }", "language": "csharp" } ] } [/block]