コード例 #1
0
        public void GetNearby_ReturnsCorrectData()
        {
            // arrange
            var models = new[]
            {
                new ZipCodeModel
                {
                    Id        = 1,
                    Zip       = 1,
                    Latitude  = 1,
                    Longitude = 1
                },
                new ZipCodeModel
                {
                    Id        = 4,
                    Zip       = 4,
                    Latitude  = 4,
                    Longitude = 4
                },
                new ZipCodeModel
                {
                    Id        = 3,
                    Zip       = 3,
                    Latitude  = 3,
                    Longitude = 3
                },
                new ZipCodeModel
                {
                    Id        = 2,
                    Zip       = 2,
                    Latitude  = 2,
                    Longitude = 2
                }
            };

            var dataStoreMock = new Mock <IDataStore>(MockBehavior.Loose);

            dataStoreMock.Setup(mock => mock.GetById <ZipCodeModel, int>(It.IsAny <int>()))
            .Returns <int>(item => models.FirstOrDefault(i => i.Id == item));
            dataStoreMock.Setup(mock => mock.GetAll <ZipCodeModel>())
            .Returns(models.AsQueryable());

            var locationServiceMock = new Mock <ILocationService>(MockBehavior.Loose);

            locationServiceMock.Setup(mock => mock.GetDistance(It.IsAny <LocationModel>(), It.IsAny <LocationModel>()))
            .Returns <LocationModel, LocationModel>((item1, item2) => Math.Sqrt(Math.Pow(item2.Latitude - item1.Latitude, 2) + Math.Pow(item2.Longitude - item1.Latitude, 2)));

            var repository = new ZipCodesRepository(
                dataStoreMock.Object,
                locationServiceMock.Object
                );

            // act
            var items = repository.GetNearby(2, 2).ToList();

            // assert
            Assert.AreEqual(2, items.Count);
        }
コード例 #2
0
        public void GetNearby_CallsDataStore()
        {
            // arrange
            var dataStoreMock = new Mock <IDataStore>(MockBehavior.Loose);

            dataStoreMock.Setup(mock => mock.GetById <ZipCodeModel, int>(It.IsAny <int>()))
            .Returns(new ZipCodeModel());

            var locationServiceMock = new Mock <ILocationService>(MockBehavior.Loose);

            var repository = new ZipCodesRepository(
                dataStoreMock.Object,
                locationServiceMock.Object
                );

            // act
            var items = repository.GetNearby(0, 1).ToList();

            // assert
            dataStoreMock.Verify(mock => mock.GetAll <ZipCodeModel>(), Times.Once());
        }