Exemplo n.º 1
0
 public TestTarget(TestDependencyRegistry testDependencyRegistry)
 {
     DependencyRegistry = testDependencyRegistry ?? throw new ArgumentNullException(nameof(testDependencyRegistry));
 }
Exemplo n.º 2
0
        public TestTargetRegistry Add(
            string environment,
            string application,
            Uri baseAddress,
            Action <TestDependencyRegistry> testDependencies = null)
        {
            if (baseAddress == null)
            {
                throw new ArgumentNullException(nameof(baseAddress));
            }

            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException("Base address must be an absolute URI.");
            }

            if (string.IsNullOrWhiteSpace(environment))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(environment));
            }

            if (string.IsNullOrWhiteSpace(application))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(application));
            }

            var container = new PocketContainer()
                            .Register(c => new HttpClient())
                            .Register(c => new TestSession(c.Resolve <IHttpContextAccessor>()))
                            .Register(c => services.GetRequiredService <IHttpContextAccessor>());

            var testDependencyRegistry = new TestDependencyRegistry(container);

            container.RegisterSingle(c => new TestTarget(testDependencyRegistry)
            {
                Application = application,
                Environment = environment,
                BaseAddress = baseAddress
            });

            container.OnFailedResolve = (t, e) =>
                                        throw new InvalidOperationException($"TestTarget does not contain registration for '{t}'.");

            if (services != null)
            {
                // fall back to application's IServiceProvider
                container.AddStrategy(type =>
                {
                    if (typeof(IPeakyTest).IsAssignableFrom(type))
                    {
                        return(null);
                    }

                    if (services.GetService(type) != null)
                    {
                        return(c => services.GetService(type));
                    }

                    return(null);
                });
            }

            testDependencies?.Invoke(testDependencyRegistry);

            container.AfterCreating <HttpClient>(client =>
            {
                if (client.BaseAddress == null)
                {
                    client.BaseAddress = baseAddress;
                }
                return(client);
            });

            targets.Add($"{environment}:{application}",
                        new Lazy <TestTarget>(() => container.Resolve <TestTarget>()));

            return(this);
        }