{"__v":2,"_id":"54af53eb0cf42a0b001d5b73","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"},"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":[],"next":{"pages":[],"description":""},"createdAt":"2015-01-09T04:07:07.740Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"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]","excerpt":"","slug":"somevalueof-and-anyvalueof","type":"basic","title":"Fuzzy Checks with Some and Any Values"}

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]