|
| 1 | +namespace Panner.AspNetCore.Tests |
| 2 | +{ |
| 3 | + using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 4 | + using NSubstitute; |
| 5 | + using System; |
| 6 | + using System.Collections; |
| 7 | + using System.Linq; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Xunit; |
| 10 | + |
| 11 | + public abstract class ParticlesModelBinderWorks<TBinder> |
| 12 | + where TBinder : IModelBinder |
| 13 | + { |
| 14 | + abstract protected TBinder Create(IPContext context); |
| 15 | + |
| 16 | + public ParticlesModelBinderWorks(){} |
| 17 | + |
| 18 | + private IPContext GetPannerContext() |
| 19 | + => Substitute.For<IPContext>(); |
| 20 | + |
| 21 | + private ModelBindingContext GetBindingContext(string modelName) |
| 22 | + { |
| 23 | + var bindingContext = Substitute.For<ModelBindingContext>(); |
| 24 | + var valueProvider = Substitute.For<IValueProvider>(); |
| 25 | + var modelStateDict = Substitute.For<ModelStateDictionary>(); |
| 26 | + |
| 27 | + bindingContext.ModelName.Returns(modelName); |
| 28 | + bindingContext.ValueProvider.Returns(valueProvider); |
| 29 | + bindingContext.ModelState.Returns(modelStateDict); |
| 30 | + |
| 31 | + return bindingContext; |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void NullIPContextThrows() |
| 36 | + { |
| 37 | + Assert.Throws<ArgumentNullException>(() => |
| 38 | + { |
| 39 | + var x = Create(null); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void NullBindingContextThrows() |
| 45 | + { |
| 46 | + var x = Create(GetPannerContext()); |
| 47 | + |
| 48 | + Assert.Throws<ArgumentNullException>(() => |
| 49 | + { |
| 50 | + x.BindModelAsync(null); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task EmptyEnumerableIfNotFoundInBindingContextAsync() |
| 56 | + { |
| 57 | + var modelName = Guid.NewGuid().ToString(); |
| 58 | + var bindingContext = GetBindingContext(modelName); |
| 59 | + |
| 60 | + bindingContext.ValueProvider.GetValue(modelName).Returns(ValueProviderResult.None); |
| 61 | + |
| 62 | + var x = Create(GetPannerContext()); |
| 63 | + await x.BindModelAsync(bindingContext); |
| 64 | + |
| 65 | + Assert.True(bindingContext.Result.IsModelSet); |
| 66 | + Assert.Empty((IEnumerable)bindingContext.Result.Model); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task FailsIfTheInputCantBeParsedByTheContext() |
| 71 | + { |
| 72 | + var modelName = Guid.NewGuid().ToString(); |
| 73 | + var valueResult = Guid.NewGuid().ToString(); |
| 74 | + |
| 75 | + var bindingContext = GetBindingContext(modelName); |
| 76 | + bindingContext.ValueProvider.GetValue(modelName).Returns(new ValueProviderResult(valueResult)); |
| 77 | + |
| 78 | + var x = Create(GetPannerContext()); |
| 79 | + await x.BindModelAsync(bindingContext); |
| 80 | + |
| 81 | + Assert.False(bindingContext.Result.IsModelSet); |
| 82 | + Assert.False(bindingContext.ModelState.IsValid); |
| 83 | + Assert.True(bindingContext.ModelState.ContainsKey(modelName)); |
| 84 | + Assert.Equal(1, bindingContext.ModelState.ErrorCount); |
| 85 | + Assert.Contains("Could not parse provided", bindingContext.ModelState[modelName].Errors.Single().ErrorMessage); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments