Ejemplo n.º 1
0
        protected void Then <T>(string id, params DomainEvent <T>[] events) where T : AggregateRoot
        {
            if (events.Length == 0)
            {
                return;
            }

            formatter.Block("Then:");

            foreach (var pair in results.Zip(events, (actual, expected) => new { actual, expected }))
            {
                var actual   = pair.actual;
                var expected = pair.expected;

                var jActual   = JObject.FromObject(actual, JsonSerializer.Create(settings));
                var jExpected = JObject.FromObject(expected, JsonSerializer.Create(settings));

                Assert(
                    actual.GetAggregateRootId().Equals(id) && JToken.DeepEquals(jActual, jExpected),
                    () => formatter.Write(expected, new EventFormatter(formatter)).NewLine(),
                    () =>
                {
                    formatter.Block("But we got this:")
                    .Indent().Write(actual, new EventFormatter(formatter)).Unindent()
                    .EndBlock();

                    var differ = new Differ();
                    var diffs  = differ.LineByLine(jActual.ToString(), jExpected.ToString());
                    var diff   = differ.PrettyLineByLine(diffs);

                    formatter
                    .NewLine().NewLine()
                    .Write("Diff:").NewLine()
                    .Write(diff);
                });
            }

            // consume events
            results = results.Skip(events.Length);
        }
Ejemplo n.º 2
0
        protected void Then(string id, params DomainEvent[] events)
        {
            if (events.Length == 0)
            {
                return;
            }

            formatter.Block("Then:");

            foreach (var expected in events)
            {
                if (results == null)
                {
                    results = Context.History.Where(x => !arrangedEvents.Contains(x.GetGlobalSequenceNumber()));
                }

                var actual = results.FirstOrDefault();

                if (actual == null)
                {
                    Assert(false,
                           () => formatter.Write(expected, new EventFormatter(formatter)).NewLine(),
                           () => formatter.Block("But we got nothing."));

                    return;
                }

                expected.Meta[DomainEvent.MetadataKeys.AggregateRootId] = id;

                var jActual   = Context.EventSerializer.Serialize(actual);
                var jExpected = Context.EventSerializer.Serialize(expected);

                Assert(
                    actual.GetAggregateRootId().Equals(id) &&
                    actual.GetType() == expected.GetType() &&
                    jActual.Data.SequenceEqual(jExpected.Data),
                    () => formatter.Write(expected, new EventFormatter(formatter)).NewLine(),
                    () =>
                {
                    formatter.Block("But we got this:")
                    .Indent().Write(actual, new EventFormatter(formatter)).Unindent()
                    .EndBlock();

                    if (!jActual.IsJson() || !jExpected.IsJson())
                    {
                        return;
                    }

                    var differ = new Differ();
                    var diffs  = differ.LineByLine(
                        Encoding.UTF8.GetString(jActual.Data),
                        Encoding.UTF8.GetString(jExpected.Data));

                    var diff = differ.PrettyLineByLine(diffs);

                    formatter
                    .NewLine().NewLine()
                    .Write("Diff:").NewLine()
                    .Write(diff).NewLine();
                });

                // consume events
                results = results.Skip(1);
            }
        }
        private void ThenAssert(string id, bool matchExact = true, params DomainEvent[] events)
        {
            if (events.Length == 0)
            {
                return;
            }

            formatter.Block("Then:");

            foreach (var expected in events)
            {
                if (results == null)
                {
                    results = Context.History.Where(x => !arrangedEvents.Contains(x.GetGlobalSequenceNumber()));
                }

                var actual = results.FirstOrDefault();

                if (actual == null)
                {
                    Assert(false,
                           () => formatter.Write(expected, new EventFormatter(formatter)).NewLine(),
                           () => formatter.Block("But we got nothing."));

                    return;
                }

                expected.Meta[DomainEvent.MetadataKeys.AggregateRootId] = id;

                if (!matchExact)
                {
                    RemoveNotProvidedPropertiesInExpectedFromActual();
                }

                var jActual   = Context.EventSerializer.Serialize(actual);
                var jExpected = Context.EventSerializer.Serialize(expected);

                Assert(
                    actual.GetAggregateRootId().Equals(id) && actual.GetType() == expected.GetType() && jActual.Data.SequenceEqual(jExpected.Data),
                    () => formatter.Write(expected, new EventFormatter(formatter)).NewLine(),
                    () => {
                    formatter.Block("But we got this:")
                    .Indent().Write(actual, new EventFormatter(formatter)).Unindent()
                    .EndBlock();

                    if (!jActual.IsJson() || !jExpected.IsJson())
                    {
                        return;
                    }

                    var differ = new Differ();
                    var diffs  = differ.LineByLine(
                        Encoding.UTF8.GetString(jActual.Data),
                        Encoding.UTF8.GetString(jExpected.Data));

                    var diff = differ.PrettyLineByLine(diffs);

                    formatter
                    .NewLine().NewLine()
                    .Write("Diff:").NewLine()
                    .Write(diff).NewLine();
                });

                // consume events
                results = results.Skip(1);



                #region Local Functions

                void RemoveNotProvidedPropertiesInExpectedFromActual()
                {
                    var expectedProperties = expected.GetType().GetProperties();

                    foreach (var expectedProperty in expectedProperties)
                    {
                        if (IsExpectedValueIgnored(out var defaultValue))
                        {
                            SetActualValueToDefaultValue();
                        }

                        bool IsExpectedValueIgnored(out object dv)
                        {
                            dv = GetDefault(expectedProperty.PropertyType);
                            var actualExpectedValue = expectedProperty.GetValue(expected);

                            return(object.Equals(actualExpectedValue, dv));
                        }

                        void SetActualValueToDefaultValue()
                        {
                            var test = System.ComponentModel.TypeDescriptor.GetProperties(actual)[expectedProperty.Name];

                            if (test != null)   //if types not same
                            {
                                test.SetValue(actual, defaultValue);
                            }
                        }
                    }
                }

                #endregion
            }
        }