Пример #1
0
 public void WithValidShouldThrowExceptionWithViewComponentIncorrectValuePredicate()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <MemoryCacheValuesComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .MemoryCache(memoryCache => memoryCache
                      .ContainingEntry(entry => entry
                                       .WithKey("test")
                                       .AndAlso()
                                       .WithValueOfType <string>()
                                       .Passing(v => v.StartsWith("inv"))))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking MemoryCacheValuesComponent expected memory cache to have entry with 'test' key and value passing the given predicate, but it failed.");
 }
Пример #2
0
 public void MemoryCacheWithBuilderShouldThrowWithDictionaryOfCacheEntriesWithInvalidKey()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <MemoryCacheValuesComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .MemoryCache(cache => cache
                      .ContainingEntries(new Dictionary <object, object>
         {
             ["test"]    = "value",
             ["invalid"] = "anotherValue"
         }))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given key, but such was not found.");
 }
Пример #3
0
 public void TempDataWithBuilderShouldThrowWithAnonymousObjectOfTempDataEntriesWithInvalidValue()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <AddTempDataComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .TempData(tempData => tempData
                   .ContainingEntries(new Dictionary <string, object>
         {
             ["Test"]    = "TempValue",
             ["Another"] = "Invalid"
         }))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking AddTempDataComponent expected temp data to have entry with 'Another' key and the provided value, but the value was different. Expected a value of 'Invalid', but in fact it was 'AnotherValue'.");
 }
Пример #4
0
 public void MemoryCacheWithBuilderShouldThrowWithDictionaryOfCacheEntriesWithInvalidValue()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <MemoryCacheValuesComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .MemoryCache(cache => cache
                      .ContainingEntries(new Dictionary <object, object>
         {
             ["test"]    = "invalid",
             ["another"] = "anotherValue"
         }))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given value, but in fact it was different. Expected a value of 'invalid', but in fact it was 'value'.");
 }
Пример #5
0
 public void TempDataWithBuilderShouldThrowWithDictionaryOfTempDataEntriesWithInvalidKey()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <AddTempDataComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .TempData(tempData => tempData
                   .ContainingEntries(new Dictionary <string, object>
         {
             ["Test"]    = "TempValue",
             ["Invalid"] = "AnotherValue"
         }))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking AddTempDataComponent expected temp data to have entry with 'Invalid' key and the provided value, but such was not found.");
 }
Пример #6
0
        public void MemoryCacheWithBuilderWithPredicateShouldThrowWithIncorrectAbsoluteExpiration()
        {
            var invalidExpirationDate = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            var actualExpirationDate  = new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc);

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <MemoryCacheValuesComponent>
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .MemoryCache(cache => cache
                             .ContainingEntry(entry => entry
                                              .WithKey("test")
                                              .WithAbsoluteExpiration(new DateTimeOffset(invalidExpirationDate))))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking MemoryCacheValuesComponent expected memory cache to have entry with 'test' key and '" + invalidExpirationDate.ToString("r", CultureInfo.InvariantCulture) + "' absolute expiration, but in fact found '" + actualExpirationDate.ToString("r", CultureInfo.InvariantCulture) + "'.");
        }
        public void MockMemoryCacheShouldBeDifferentForEveryViewComponentCallSynchronously()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services => services.AddMemoryCache());

            // second call should not have cache entries
            MyViewComponent <MemoryCacheComponent>
            .Instance()
            .WithMemoryCache(cache => cache.WithEntry("test", "value"))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View();

            MyViewComponent <MemoryCacheComponent>
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .Content("No cache");

            MyApplication.StartsFrom <DefaultStartup>();
        }
Пример #8
0
        public void SessionWithNoNumberShouldNotThrowExceptionWithAnyEntries()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <AddSessionComponent>
            .InvokedWith(c => c.Invoke())
            .ShouldHave()
            .Session()
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
Пример #9
0
        public void MemoryCacheWithBuilderWithPredicateShouldThrowWithIncorrectSlidingExpiration()
        {
            var invalidSlidingExpiration = TimeSpan.FromMinutes(1);
            var actualSlidingExpiration  = TimeSpan.FromMinutes(5);

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <MemoryCacheValuesComponent>
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .MemoryCache(cache => cache
                             .ContainingEntry(entry => entry
                                              .WithKey("test")
                                              .WithSlidingExpiration(invalidSlidingExpiration)))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking MemoryCacheValuesComponent expected memory cache to have entry with 'test' key and '" + invalidSlidingExpiration.ToString() + "' sliding expiration, but in fact found '" + actualSlidingExpiration.ToString() + "'.");
        }
Пример #10
0
        public void MemoryCacheWithBuilderWithPredicateShouldThrowWithIncorrectPriority()
        {
            var invalidPriority = CacheItemPriority.Low;
            var actualPriority  = CacheItemPriority.High;

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <MemoryCacheValuesComponent>
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .MemoryCache(cache => cache
                             .ContainingEntry(entry => entry
                                              .WithKey("test")
                                              .WithPriority(invalidPriority)))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking MemoryCacheValuesComponent expected memory cache to have entry with 'test' key and " + invalidPriority.ToString() + " priority, but in fact found " + actualPriority.ToString() + ".");
        }
Пример #11
0
 public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsAndIncorrectEntryValue()
 {
     Test.AssertException <DataProviderAssertionException>(
         () =>
     {
         MyViewComponent <MemoryCacheValuesComponent>
         .InvokedWith(c => c.Invoke())
         .ShouldHave()
         .MemoryCache(cache => cache
                      .ContainingEntry("test", "invalid", new MemoryCacheEntryOptions
         {
             AbsoluteExpiration = new DateTimeOffset(new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc)),
             AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1),
             Priority          = CacheItemPriority.High,
             SlidingExpiration = TimeSpan.FromMinutes(5)
         }))
         .AndAlso()
         .ShouldReturn()
         .View();
     },
         "When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given value, but in fact it was different.");
 }
Пример #12
0
 public void ViewComponentInvocationShouldNotThrowExceptionWithCorrectResponse()
 {
     MyViewComponent <HttpResponseComponent>
     .InvokedWith(c => c.Invoke())
     .ShouldHave()
     .HttpResponse(response => response
                   .WithContentType(ContentType.ApplicationJson)
                   .AndAlso()
                   .WithContentLength(100)
                   .AndAlso()
                   .WithStatusCode(HttpStatusCode.InternalServerError)
                   .AndAlso()
                   .ContainingHeader("TestHeader", "TestHeaderValue")
                   .ContainingCookie("TestCookie", "TestCookieValue", new CookieOptions
     {
         HttpOnly = true,
         Secure   = true,
         Domain   = "testdomain.com",
         Expires  = new DateTimeOffset(new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc)),
         Path     = "/"
     }));
 }
        public void DbContextShouldNotThrowExceptionWithCorrectPredicate()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddDbContext <CustomDbContext>(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName));
            });

            MyViewComponent <CreateDataComponent>
            .InvokedWith(c => c.Invoke(new CustomModel {
                Id = 1, Name = "Test"
            }))
            .ShouldHave()
            .Data(dbContext => dbContext
                  .WithEntities <CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null))
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
Пример #14
0
        public void SessionWithBuilderShouldWorkCorrectly()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <AddSessionComponent>
            .InvokedWith(c => c.Invoke())
            .ShouldHave()
            .Session(session => session
                     .ContainingEntry("Integer", 1))
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
        public void DbContextShouldThrowExceptionWithIncorrectPredicate()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddDbContext <CustomDbContext>(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName));
            });

            MyViewComponent <CreateDataComponent>
            .InvokedWith(c => c.Invoke(new CustomModel {
                Id = 1, Name = "Test"
            }))
            .ShouldHave()
            .Data(dbContext => dbContext
                  .WithEntities <CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null))
            .AndAlso()
            .ShouldReturn()
            .View();

            Test.AssertException <DataProviderAssertionException>(() =>
            {
                MyViewComponent <CreateDataComponent>
                .InvokedWith(c => c.Invoke(new CustomModel {
                    Id = 2, Name = "Test"
                }))
                .ShouldHave()
                .Data(dbContext => dbContext
                      .WithEntities <CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) == null))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                                                                  "When invoking CreateDataComponent expected the CustomDbContext entities to pass the given predicate, but it failed.");

            MyApplication.StartsFrom <DefaultStartup>();
        }
        public void ContainingEntryWithKeyShouldNotThrowExceptionWithCorrectViewComponentEntry()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <AddSessionComponent>
            .InvokedWith(c => c.Invoke())
            .ShouldHave()
            .Session(session => session
                     .ContainingEntryWithKey("Integer")
                     .AndAlso()
                     .ContainingEntryWithKey("String"))
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
        public void WithOptionsShouldSetCorrectOptions()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                var configuration = new ConfigurationBuilder()
                                    .AddJsonFile("config.json")
                                    .Build();

                services.Configure <CustomSettings>(configuration.GetSection("Settings"));
            });

            MyViewComponent <OptionsComponent>
            .Instance()
            .WithOptions(options => options
                         .For <CustomSettings>(settings => settings.Name = "Test"))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View();

            MyViewComponent <OptionsComponent>
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .Content();

            MyViewComponent <OptionsComponent>
            .Instance()
            .WithOptions(options => options
                         .For <CustomSettings>(settings => settings.Name = "Invalid"))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .Content();

            MyApplication.StartsFrom <DefaultStartup>();
        }
Пример #18
0
        public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInvalidSlidingExpiration()
        {
            int invalidSlidingExpirationValue = 1;

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <MemoryCacheValuesComponent>
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .MemoryCache(cache => cache
                             .ContainingEntry("test", "value", new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = new DateTimeOffset(new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc)),
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1),
                    Priority          = CacheItemPriority.High,
                    SlidingExpiration = TimeSpan.FromMinutes(invalidSlidingExpirationValue)
                }))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given options, but in fact they were different. Difference occurs at 'MemoryCacheEntryOptions.SlidingExpiration.== (Equality Operator)'. Expected a value of '00:01:00', but in fact it was '00:05:00'.");
        }
Пример #19
0
 public void HttpResponsePredicateShouldWorkCorrectly()
 {
     MyViewComponent <NormalComponent>
     .InvokedWith(c => c.Invoke())
     .ShouldPassForThe <HttpResponse>(response => response != null);
 }