public void CreateBindingDelegate_CalledWhenNoInnerBinders_ExpectArgumentExceptionWithCorrectParamName(
     Type requestType, ServiceRequestBinderContext context)
 {
     new ServiceRequestBinderChain(new IServiceRequestBinder[0])
     .Invoking(x => x.CreateBindingDelegate(requestType, context))
     .ShouldThrow <ArgumentException>().And.ParamName.Should().Be("requestType");
 }
        public void CreateBindingDelegate_CalledMultipleTimes_ExpectEnumerableOfInnerBindersIsOnlyEnumeratedOnce(
            Type requestType1, ServiceRequestBinderContext context1, Type requestType2, ServiceRequestBinderContext context2)
        {
            var innerBinders = Mock.Enumerable <IServiceRequestBinder>();
            var chain        = new ServiceRequestBinderChain(innerBinders);

            chain.Invoking(x => x.CreateBindingDelegate(requestType1, context1)).ShouldThrow <Exception>();
            chain.Invoking(x => x.CreateBindingDelegate(requestType2, context2)).ShouldThrow <Exception>();

            innerBinders.AssertWasCalled(x => x.GetEnumerator(), x => x.Repeat.Once());
        }
        public void CreateBindingDelegate_Called_ExpectReturnedDelegateCallsGenericVersionOfDelegatePassedToConstructor(
            ServiceRequestBinderContext context, object requestParameters)
        {
            var binder = new NancyModelServiceRequestBinder(m => m.StubNancyModelBind <SomethingOtherThanRequest>());
            var lambda = binder.CreateBindingDelegate(typeof(Request), context);

            StubNancyModelBinding.StubBoundModel = new Request();
            StubNancyModelBinding.SpiedModule    = null;

            lambda(requestParameters).Should().BeSameAs(StubNancyModelBinding.StubBoundModel);
            StubNancyModelBinding.SpiedModule.Should().BeSameAs(context.NancyModule);
        }
        public void CreateBindingDelegate_CalledWhenAtLeastOneInnerBinderCanCreateDelegate_ExpectReturnedDelegateIsFromInnerBinder(
            [WithinInclusiveRange(0, 10)] int numberOfFalseInnerBinders,
            [WithinInclusiveRange(1, 10)] int numberOfTrueInnerBinders,
            Type requestType,
            ServiceRequestBinderContext context)
        {
            Func <object, object> constructedDelegate = x => new object();
            var falseBinders = CreateNumberOfStubServiceRequestBindersUnableToCreateDelegate(numberOfFalseInnerBinders, requestType);
            var trueBinders  = CreateNumberOfStubServiceRequestBindersAbleToCreateDelegate(numberOfTrueInnerBinders, requestType);

            trueBinders.ForEach(inv => inv.Stub(
                                    x => x.CreateBindingDelegate(Arg <Type> .Is.Same(requestType), Arg <ServiceRequestBinderContext> .Is.Same(context)))
                                .Return(constructedDelegate));

            var chain = new ServiceRequestBinderChain(falseBinders.Concat(trueBinders).Shuffle());

            chain.CreateBindingDelegate(requestType, context).Should().BeSameAs(constructedDelegate);
        }
		public void CreateBindingDelegate_CalledWithNullRequestType_ExpectArgumentNullExceptionWithCorrectParamName(
			ServiceRequestBinderContext context)
		{
			CreateChainWithDummyDependencies().Invoking(x => x.CreateBindingDelegate(null, context))
				.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("requestType");
		}
		public void CreateBindingDelegate_CalledMultipleTimes_ExpectEnumerableOfInnerBindersIsOnlyEnumeratedOnce(
			Type requestType1, ServiceRequestBinderContext context1, Type requestType2, ServiceRequestBinderContext context2)
		{
			var innerBinders = Mock.Enumerable<IServiceRequestBinder>();
			var chain = new ServiceRequestBinderChain(innerBinders);
			chain.Invoking(x => x.CreateBindingDelegate(requestType1, context1)).ShouldThrow<Exception>();
			chain.Invoking(x => x.CreateBindingDelegate(requestType2, context2)).ShouldThrow<Exception>();

			innerBinders.AssertWasCalled(x => x.GetEnumerator(), x => x.Repeat.Once());
		}
		public void CreateBindingDelegate_CalledWhenAtLeastOneInnerBinderCanCreateDelegate_ExpectReturnedDelegateIsFromInnerBinder(
			[WithinInclusiveRange(0, 10)] int numberOfFalseInnerBinders,
			[WithinInclusiveRange(1, 10)] int numberOfTrueInnerBinders,
			Type requestType,
			ServiceRequestBinderContext context)
		{
			Func<object, object> constructedDelegate = x => new object();
			var falseBinders = CreateNumberOfStubServiceRequestBindersUnableToCreateDelegate(numberOfFalseInnerBinders, requestType);
			var trueBinders = CreateNumberOfStubServiceRequestBindersAbleToCreateDelegate(numberOfTrueInnerBinders, requestType);
			trueBinders.ForEach(inv => inv.Stub(
				x => x.CreateBindingDelegate(Arg<Type>.Is.Same(requestType), Arg<ServiceRequestBinderContext>.Is.Same(context)))
					.Return(constructedDelegate));

			var chain = new ServiceRequestBinderChain(falseBinders.Concat(trueBinders).Shuffle());
			chain.CreateBindingDelegate(requestType, context).Should().BeSameAs(constructedDelegate);
		}
		public void CreateBindingDelegate_CalledWhenNoInnerBindersCanCreateDelegate_ExpectArgumentExceptionWithCorrectParamName(
			[WithinInclusiveRange(1, 10)] int numberOfInnerBinders, Type requestType, ServiceRequestBinderContext context)
		{
			var innerBinders = CreateNumberOfStubServiceRequestBindersUnableToCreateDelegate(numberOfInnerBinders, requestType);
			new ServiceRequestBinderChain(innerBinders)
				.Invoking(x => x.CreateBindingDelegate(requestType, context))
					.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("requestType");
		}
		public void CreateBindingDelegate_CalledWhenNoInnerBinders_ExpectArgumentExceptionWithCorrectParamName(
			Type requestType, ServiceRequestBinderContext context)
		{
			new ServiceRequestBinderChain(new IServiceRequestBinder[0])
				.Invoking(x => x.CreateBindingDelegate(requestType, context))
					.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("requestType");
		}
 public void CreateBindingDelegate_CalledWithConcreteClassType_ExpectDelegateIsReturned(
     Type concreteType, ServiceRequestBinderContext context)
 {
     new NancyModelServiceRequestBinder().Invoking(x => x.CreateBindingDelegate(concreteType, context))
     .Should().NotBeNull();
 }
 public void CreateBindingDelegate_CalledWithNonConcreteType_ExpectArgumentExceptionWithCorrectParamName(
     Type nonConcreteType, ServiceRequestBinderContext context)
 {
     new NancyModelServiceRequestBinder().Invoking(x => x.CreateBindingDelegate(nonConcreteType, context))
     .ShouldThrow <ArgumentException>().And.ParamName.Should().Be("requestType");
 }
        public void CreateBindingDelegate_CalledWhenNoInnerBindersCanCreateDelegate_ExpectArgumentExceptionWithCorrectParamName(
            [WithinInclusiveRange(1, 10)] int numberOfInnerBinders, Type requestType, ServiceRequestBinderContext context)
        {
            var innerBinders = CreateNumberOfStubServiceRequestBindersUnableToCreateDelegate(numberOfInnerBinders, requestType);

            new ServiceRequestBinderChain(innerBinders)
            .Invoking(x => x.CreateBindingDelegate(requestType, context))
            .ShouldThrow <ArgumentException>().And.ParamName.Should().Be("requestType");
        }
 public void CreateBindingDelegate_CalledWithNullRequestType_ExpectArgumentNullExceptionWithCorrectParamName(
     ServiceRequestBinderContext context)
 {
     CreateChainWithDummyDependencies().Invoking(x => x.CreateBindingDelegate(null, context))
     .ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("requestType");
 }