Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestContext"/> struct.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="sourceFile">The source file.</param>
        /// <param name="subDirectory">The sub directory.</param>
        /// <param name="reporter">The reporter.</param>
        public TestContext(string methodName, string className, string sourceFile, string subDirectory, ReporterAttribute reporter)
        {
            if (string.IsNullOrEmpty(sourceFile))
            {
                throw new System.ArgumentNullException(nameof(sourceFile), $"Could not resolve {className} source file.");
            }

            ReporterAttribute = reporter;
            TestClassName     = className;
            TestMethodName    = methodName;
            SubDirectory      = subDirectory ?? string.Empty;
            SourceDirectory   = Path.GetDirectoryName(sourceFile);
        }
Ejemplo n.º 2
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));
            }
        }