Fuzzy Checks with Some and Any Values

📘

NOTE

The techniques shown here only work with lambda-based partial matching. They do not work with anonymous objects.

The ShouldLookLike assertion also allows you to do fuzzy matching. For example, you can check to make sure that a property isn't null:

[Test]
            public void then_you_can_check_for_non_null_values()
            {
                var engine = new Engine { Maker = "Heroic" };
                engine.ShouldLookLike(() => new Engine
                {
                    Maker = Any.NonNullValueOf<string>()
                });
            }

You can also check for a specific value in a range:

[Test]
            public void then_you_can_check_for_values_in_a_range()
            {
                var engine = new Engine {YearBuilt = 2015};
                engine.ShouldLookLike(() => new Engine
                {
                    YearBuilt = Some.ValueInRange(2014, 2016)
                });
            }

Or for a value matching some arbitrary criteria:

[Test]
            public void then_you_can_check_for_a_value_matching_some_expression()
            {
                var engine = new Engine {Maker = "Heroic"};
                engine.ShouldLookLike(() => new Engine
                {
                    Maker = Some.ValueOf<string>(s => char.IsUpper(s[0]))
                });
            }

You can also check to make sure a list contains some value (or even a partial value!)

[Test]
            public void then_you_can_check_for_an_item_in_a_list()
            {
                var warehouse = new Warehouse
                {
                    Engines = new[] {new Engine {YearBuilt = 2013}, new Engine {YearBuilt = 2016}}
                };

                warehouse.ShouldLookLike(() => new Warehouse
                {
                    Engines = Some.ListContaining(() => new Engine
                    {
                        //Yep, you can use partial matching recursively!
                        YearBuilt = Some.ValueOf<int>(i => i % 2 == 0)
                    })
                });
            }

Did this page help you?