コード例 #1
0
ファイル: OwnersTests.cs プロジェクト: wayne-scott/pets
        public void CheckThatTheCatListOnlyContiansCats()
        {
            var ownerWithCat = new Owner();

            ownerWithCat.Pets.Add(new Pet {
                Name = "Tigger", Type = "Cat"
            });
            var ownerWithOutCat = new Owner();

            ownerWithOutCat.Pets.Add(new Pet {
                Name = "Arnold", Type = "Aardvark"
            });

            var listOfOwners = new List <Owner>
            {
                ownerWithCat,
                ownerWithOutCat
            };

            var owners = new Owners();

            owners.Initialise(listOfOwners);

            Assert.AreEqual(2, owners.PetOwners.Count);
            Assert.AreEqual(1, owners.CatOwners.Count());
        }
コード例 #2
0
        public void OnGetWithOwnerNameNoMatch()
        {
            var repository = Substitute.For <IOwnerRepository>();
            var owners     = new Owners();

            owners.Initialise(new List <DomainModel.Owner> {
                new DomainModel.Owner {
                    Name = "John"
                }
            });
            repository.GetAllOwners().Returns(owners);

            var indexModel = new IndexModel(repository);

            Assert.IsNotNull(indexModel);
            indexModel.OnGet("Fred");

            Assert.IsNull(indexModel.Owner);
            Assert.IsNull(indexModel.Owners);
            Assert.AreEqual("Fred not found?", indexModel.Message);
            repository.Received(1).GetAllOwners();
        }
コード例 #3
0
        public void OnGet()
        {
            var repository = Substitute.For <IOwnerRepository>();
            var owners     = new Owners();

            owners.Initialise(new List <DomainModel.Owner> {
                new DomainModel.Owner {
                    Name = "John"
                }
            });
            repository.GetAllOwners().Returns(owners);

            var indexModel = new IndexModel(repository);

            Assert.IsNotNull(indexModel);
            indexModel.OnGet(string.Empty);

            Assert.IsNull(indexModel.Owner);
            Assert.IsNotNull(indexModel.Owners);
            Assert.AreEqual(1, indexModel.Owners.Count);
            Assert.AreEqual(string.Empty, indexModel.Message);
            repository.Received(1).GetAllOwners();
        }
コード例 #4
0
        /// <summary>
        /// Provide access to all owners.
        /// </summary>
        /// <returns>A <see cref="Owners"/> <see langword="object"/>.</returns>
        public Owners GetAllOwners()
        {
            var owners = new Owners();

            if (!_memoryCache.TryGetValue(EndpointURL, out List <Owner> listOfOwners))
            {
                var task = HttpClient.GetAsync(EndpointURL)
                           .ContinueWith(taskwithresponse =>
                {
                    var response = taskwithresponse.Result;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        var jsonString = response.Content.ReadAsStringAsync();
                        jsonString.Wait();
                        listOfOwners = ValidateAndParseResponse(jsonString.Result);
                    }
                });
                task.Wait();
                _memoryCache.Set(EndpointURL, listOfOwners, TimeSpan.FromMinutes(60));
            }
            owners.Initialise(listOfOwners);
            return(owners);
        }