Example #1
0
        public void TestAssertThatWithJsonComparer()
        {
            var expectedJson = JObject.FromObject(new { parent = new { __capture = new { name = "some-capture-name", type = "integer" } } });
            var actualJson   = JObject.FromObject(new { parent = 42 });

            var comparerCalled = false;
            var jsonComparer   = JsonComparer.GetDefault((s, token) => { comparerCalled = true; });

            Assert.That(actualJson, IsJson.EquivalentTo(expectedJson).WithComparer(jsonComparer));
            Assert.That(comparerCalled, Is.True);
        }
Example #2
0
        public void TestAssertThat()
        {
            const string expectedJson = @"{
                ""a"":1,
                ""b"":""abc""
            }";
            const string actualJson   = @"{
                ""a"":42,
                ""b"":""abc""
            }";

            Assert.That(actualJson, IsJson.EquivalentTo(expectedJson));
        }
Example #3
0
        public void TestAssertThatMatchDateWithoutParsingThem()
        {
            const string expectedJson = @"{
                ""date"":{
                    ""__match"":{
                        ""regex"": ""^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{7}Z?""
                    }
                }
            }";
            const string actualJson   = @"{
                ""date"": ""2042-05-04T06:01:06.0000000Z""
            }";

            Assert.That(actualJson, IsJson.EquivalentTo(expectedJson));
        }
Example #4
0
        public void TheResponseShouldContainsAJsonArrayContainingTheFollowingElementIdentifiedBy(string identityField, string expectedJson)
        {
            var content = _scenarioContext.GetLastHttpResponseContent();

            JObject expectedObject;

            try
            {
                expectedObject = JObject.Parse(expectedJson);
            }
            catch (JsonReaderException ex)
            {
                throw new Exception($"Invalid expected JSON: {ex.Message} Line:\n '{expectedJson.Split('\n')[ex.LineNumber - 1]}'", ex);
            }

            var identityValue = (JValue)expectedObject.Property(identityField).Value;
            var array         = JArray.Parse(content);

            foreach (var element in array)
            {
                if (element.Type != JTokenType.Object || !(element is JObject actualObject))
                {
                    continue;
                }

                if (!identityValue.Equals((JValue)actualObject.Property(identityField).Value))
                {
                    continue;
                }

                Assert.That(element, IsJson.EquivalentTo(expectedObject).WithComparer(_jsonComparer).WithColoredOutput());
                return;
            }

            Assert.Fail($"Failed to find expected element using `{identityField}`.\nExpected identifier value: `{identityValue}` but found values: {string.Join(",", array.Select(s => s[identityField]))}\nResponse:\n{content}");
        }
Example #5
0
        public void TheResponseShouldContainsTheFollowingJson(JToken expectedJson)
        {
            var jsonContent = _scenarioContext.GetLastJsonHttpResponseContent();

            Assert.That(jsonContent, IsJson.EquivalentTo(expectedJson).WithComparer(_jsonComparer).WithColoredOutput());
        }