Пример #1
0
        /// <summary>
        /// Bind the incoming request to an existing instance
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="instance">The class instance to bind properties to</param>
        /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
        /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
        public static TModel BindTo <TModel>(this INancyModule module, TModel instance, BindingConfig configuration, params string[] blacklistedProperties)
        {
            dynamic adapter =
                new DynamicModelBinderAdapter(module.ModelBinderLocator, module.Context, instance, configuration, blacklistedProperties);

            return(adapter);
        }
        public void Should_pass_type_to_locator_when_cast_implicitly()
        {
            // Given
            var fakeModelBinder = A.Fake<IModelBinder>();
            var returnModel = new Model();
            A.CallTo(() => fakeModelBinder.Bind(null, null, null, null)).WithAnyArguments().Returns(returnModel);

            var fakeLocator = A.Fake<IModelBinderLocator>();
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
            dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext(), null, A.Dummy<BindingConfig>());

            // When
            Model result = adapter;

            // Then
            A.CallTo(() => fakeLocator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Should_return_object_from_binder_if_binder_doesnt_return_null()
        {
            // Given
            var fakeModelBinder = A.Fake<IModelBinder>();
            var returnModel = new Model();
            A.CallTo(() => fakeModelBinder.Bind(null, null, null)).WithAnyArguments().Returns(returnModel);

            var fakeLocator = A.Fake<IModelBinderLocator>();
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
            dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext());

            // When
            Model result = adapter;

            // Then
            result.ShouldNotBeNull();
            result.ShouldBeSameAs(returnModel);
        }
        public void Should_invoke_binder_with_context()
        {
            // Given
            var context = new NancyContext();

            var fakeModelBinder = A.Fake<IModelBinder>();
            var returnModel = new Model();
            A.CallTo(() => fakeModelBinder.Bind(null, null, null, null)).WithAnyArguments().Returns(returnModel);

            var fakeLocator = A.Fake<IModelBinderLocator>();
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, context)).Returns(fakeModelBinder);
            dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, context, null, A.Dummy<BindingConfig>());

            // When
            Model result = adapter;

            // Then
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, context)).MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Should_pass_blacklist_to_binder()
        {
            // Given
            var context = new NancyContext();
            var instance = new Model();
            var config = BindingConfig.Default;

            var fakeModelBinder = A.Fake<IModelBinder>();
            var returnModel = new Model();
            A.CallTo(() => fakeModelBinder.Bind(null, null, null, null)).WithAnyArguments().Returns(returnModel);

            var fakeLocator = A.Fake<IModelBinderLocator>();
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
            dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, context, instance, config);

            // When
            Model result = adapter;

            // Then
            A.CallTo(() => fakeModelBinder.Bind(A<NancyContext>._, A<Type>._, A<object>._, config, A<string[]>._)).MustHaveHappened();
        }
        public void Should_throw_if_locator_does_not_return_binder()
        {
            // Given
            var fakeLocator = A.Fake<IModelBinderLocator>();
            A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(null);
            dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext(), null, A.Dummy<BindingConfig>());

            // When
            var result = Record.Exception(() => (Model)adapter);

            // Then
            result.ShouldBeOfType(typeof(ModelBindingException));
        }