コード例 #1
0
        /// <summary>
        /// Assert that the serialized <paramref name="subject" /> is equal to it's approved file.
        /// </summary>
        /// <param name="approver">The approver.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="testParameters">The test parameters supplied by parameterized test.</param>
        /// <param name="fileExtension">The file extension (with or without a dot).</param>
        /// <param name="reporter">The reporter.</param>
        /// <param name="fileResolver">The file resolver.</param>
        /// <exception cref="Acklann.Diffa.Exceptions.ResultNotApprovedException">The subject did not match the approved-file.</exception>
        public static void Approve(IApprover approver, object subject, object[] testParameters, string fileExtension = ".txt", IReporter reporter = default, IFileResolver fileResolver = default)
        {
            var contextBuilder = new StackTraceParser();

            if (fileResolver == null)
            {
                fileResolver = new ContextualFileResolver(contextBuilder);
            }

            string resultFile   = fileResolver.GetResultFilePath(fileExtension, testParameters);
            string approvedFile = fileResolver.GetApprovedFilePath(fileExtension, testParameters);

            if (approver.Approve(subject, resultFile, approvedFile, out string reasonWhyItWasNotApproved) == false)
            {
                if (_shouldReport)
                {
                    if (reporter == null)
                    {
                        ReporterAttribute attribute = contextBuilder.Context.ReporterAttribute;
                        if (attribute?.Reporter == null)
                        {
                            reporter = _reporterFactory.GetFirstAvailableReporter(attribute.ShouldInterrupt);
                        }
                        else
                        {
                            reporter = (IReporter)Activator.CreateInstance(attribute.Reporter, args: attribute.ShouldInterrupt);
                        }
                    }

                    if (reporter.Launch(resultFile, approvedFile))
                    {
                        // Checking the results again because the user may have updated the approved file when the reporter was launched.
                        if (approver.Approve(subject, resultFile, approvedFile, out reasonWhyItWasNotApproved))
                        {
                            return;
                        }
                    }
                }

                throw new ResultNotApprovedException(ExceptionMessage.GetResultWasNotApproved(resultFile, approvedFile, reasonWhyItWasNotApproved));
            }
        }