public void CreateWithNullContainerWillThrow()
 {
     // Fixture setup
     var sut = new ConstructorInvoker(new ModestConstructorQuery());
     var dummyRequest = new object();
     // Exercise system and verify outcome
     Assert.Throws<ArgumentNullException>(() =>
         sut.Create(dummyRequest, null));
     // Teardown
 }
 public void CreateWithNullRequestWillReturnNull()
 {
     // Fixture setup
     var sut = new ConstructorInvoker(new ModestConstructorQuery());
     // Exercise system
     var dummyContainer = new DelegatingSpecimenContext();
     var result = sut.Create(null, dummyContainer);
     // Verify outcome
     var expectedResult = new NoSpecimen();
     Assert.Equal(expectedResult, result);
     // Teardown
 }
 public void CreateReturnsCorrectResultWhenQueryReturnsEmptyConstructors()
 {
     // Fixture setup
     var dummyRequest = typeof(object);
     var query = new DelegatingConstructorQuery { OnSelectConstructors = t => Enumerable.Empty<IMethod>() };
     var sut = new ConstructorInvoker(query);
     // Exercise system
     var dummyContext = new DelegatingSpecimenContext();
     var result = sut.Create(dummyRequest, dummyContext);
     // Verify outcome
     var expectedResult = new NoSpecimen(dummyRequest);
     Assert.Equal(expectedResult, result);
     // Teardown
 }
        public void CreateFromTypeWillUseFirstConstructorItCanSatisfy()
        {
            // Fixture setup
            var requestedType = typeof(MultiUnorderedConstructorType);
            var ctor1 = requestedType.GetConstructor(new[] { typeof(MultiUnorderedConstructorType.ParameterObject) });
            var ctor2 = requestedType.GetConstructor(new[] { typeof(string), typeof(int) });

            var picker = new DelegatingConstructorQuery { OnSelectConstructors = t => new IMethod[] { new ConstructorMethod(ctor1), new ConstructorMethod(ctor2) } };
            var sut = new ConstructorInvoker(picker);

            var ctor2Params = ctor2.GetParameters();
            var expectedText = "Anonymous text";
            var expectedNumber = 14;

            var context = new DelegatingSpecimenContext();
            context.OnResolve = r =>
                {
                    if (ctor2Params.Any(r.Equals))
                    {
                        var pType = ((ParameterInfo)r).ParameterType;
                        if (typeof(string) == pType)
                        {
                            return expectedText;
                        }
                        if (typeof(int) == pType)
                        {
                            return expectedNumber;
                        }
                    }
                    return new NoSpecimen(r);
                };
            // Exercise system
            var result = sut.Create(requestedType, context);
            // Verify outcome
            var actual = Assert.IsAssignableFrom<MultiUnorderedConstructorType>(result);
            Assert.Equal(expectedText, actual.Text);
            Assert.Equal(expectedNumber, actual.Number);
            // Teardown
        }
        public void CreateFromTypeWillInvokeContainerCorrectly()
        {
            // Fixture setup
            var requestedType = typeof(DoubleParameterType<long, short>);
            var parameters = requestedType.GetConstructors().Single().GetParameters();

            var mockVerified = false;
            var contextMock = new DelegatingSpecimenContext();
            contextMock.OnResolve = r =>
                {
                    if (parameters.Any(r.Equals))
                    {
                        mockVerified = true;
                        var pType = ((ParameterInfo)r).ParameterType;
                        if (typeof(long) == pType)
                        {
                            return new long();
                        }
                        if (typeof(short) == pType)
                        {
                            return new short();
                        }
                    }
                    throw new ArgumentException("Unexpected container request.", "r");
                };

            var sut = new ConstructorInvoker(new ModestConstructorQuery());
            // Exercise system
            sut.Create(requestedType, contextMock);
            // Verify outcome
            Assert.True(mockVerified, "Mock verification");
            // Teardown
        }
        public void CreateFromTypeWhenParentCanGenerateBothParametersWillReturnCorrectResult()
        {
            // Fixture setup
            var expectedParameterValues = new object[] { 1, 2m };
            var parameterQueue = new Queue<object>(expectedParameterValues);

            var requestedType = typeof(DoubleParameterType<int, decimal>);
            var parameters = requestedType.GetConstructors().Single().GetParameters();

            var container = new DelegatingSpecimenContext();
            container.OnResolve = r =>
            {
                if (parameters.Any(r.Equals))
                {
                    return parameterQueue.Dequeue();
                }
                return null;
            };

            var sut = new ConstructorInvoker(new ModestConstructorQuery());
            // Exercise system
            var result = sut.Create(requestedType, container);
            // Verify outcome
            var actual = (DoubleParameterType<int, decimal>)result;
            Assert.Equal(expectedParameterValues[0], actual.Parameter1);
            Assert.Equal(expectedParameterValues[1], actual.Parameter2);
            // Teardown
        }
 public void CreateFromTypeWhenParentCanGenerateOneParameterButNotTheOtherWillReturnCorrectNull()
 {
     // Fixture setup
     var requestedType = typeof(DoubleParameterType<string, int>);
     var parameters = requestedType.GetConstructors().Single().GetParameters();
     var container = new DelegatingSpecimenContext { OnResolve = r => parameters[0] == r ? new object() : new NoSpecimen(r) };
     var sut = new ConstructorInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(requestedType, container);
     // Verify outcome
     Assert.IsAssignableFrom<NoSpecimen>(result);
     // Teardown
 }
 public void CreateFromTypeWithNoPublicConstructorWhenContainerCanSatisfyRequestReturnsCorrectResult()
 {
     // Fixture setup
     var container = new DelegatingSpecimenContext { OnResolve = r => new object() };
     var sut = new ConstructorInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(typeof(AbstractType), container);
     // Verify outcome
     var expectedResult = new NoSpecimen(typeof(AbstractType));
     Assert.Equal(expectedResult, result);
     // Teardown
 }
 public void CreateFromTypeRequestWhenContainerCannotSatisfyParameterRequestWillReturnCorrectResult()
 {
     // Fixture setup
     var type = typeof(string);
     var container = new DelegatingSpecimenContext { OnResolve = r => new NoSpecimen(type) };
     var sut = new ConstructorInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(type, container);
     // Verify outcome
     var expectedResult = new NoSpecimen(type);
     Assert.Equal(expectedResult, result);
     // Teardown
 }