public void Throw_ArgumentNullException_When_SupplierModels_IsNull()
        {
            // Arrange
            var ctxStub            = new Mock <IOnlineStoreContext>();
            var addressServiceStub = new Mock <IAddressService>();
            var townServiceStub    = new Mock <ITownService>();
            var mapperStub         = new Mock <IMapper>();

            var supplierService = new SupplierService(ctxStub.Object, addressServiceStub.Object, townServiceStub.Object, mapperStub.Object);

            Action executingAddSupplierRangeMethod = () => supplierService.AddSupplierRange(null);

            // Act & Assert
            Assert.ThrowsException <ArgumentNullException>(executingAddSupplierRangeMethod);
        }
        public void Invoke_TownServiceCreate_When_SupplierModelTown_DoesNotExists_InDatabase()
        {
            // Arrange
            var fakeAddressText          = "testAddress";
            var fakeTownName             = "testTown";
            var fakeSupplierImportModel  = new Mock <ISuppliersImportModel>();
            var fakeSupplierImportModels = new List <ISuppliersImportModel>()
            {
                fakeSupplierImportModel.Object
            };

            var fakeTown = new Town()
            {
                Name = fakeTownName
            };
            var fakeTowns = new List <Town>()
            {
            }.GetQueryableMockDbSet();
            var newFakeTowns = new List <Town>()
            {
                fakeTown
            }.GetQueryableMockDbSet();

            var fakeAddress = new Address()
            {
                AddressText = fakeAddressText, Town = fakeTown
            };
            var fakeAddresses = new List <Address>()
            {
            }.GetQueryableMockDbSet();
            var newfakeAddresses = new List <Address>()
            {
                fakeAddress
            }.GetQueryableMockDbSet();

            var fakeSupplier  = new Mock <Supplier>();
            var fakeSuppliers = new List <Supplier>()
            {
            }.GetQueryableMockDbSet();

            var ctxStub            = new Mock <IOnlineStoreContext>();
            var mockTownService    = new Mock <ITownService>();
            var addressServiceStub = new Mock <IAddressService>();
            var mapperStub         = new Mock <IMapper>();

            var supplierService = new SupplierService(ctxStub.Object, addressServiceStub.Object, mockTownService.Object, mapperStub.Object);

            Action addingTownToTowns =
                () =>
                ctxStub
                .Setup(ctx => ctx.Towns)
                .Returns(newFakeTowns.Object);

            Action addingAddressToAddresses =
                () =>
                ctxStub
                .Setup(ctx => ctx.Addresses)
                .Returns(newfakeAddresses.Object);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Address)
            .Returns(fakeAddressText);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Town)
            .Returns(fakeTownName);

            mapperStub
            .Setup(m => m.Map <ISuppliersImportModel, Supplier>(fakeSupplierImportModel.Object))
            .Returns(fakeSupplier.Object);

            ctxStub
            .Setup(ctx => ctx.Towns)
            .Returns(fakeTowns.Object);

            ctxStub
            .Setup(ctx => ctx.Addresses)
            .Returns(fakeAddresses.Object);

            ctxStub
            .Setup(ctx => ctx.Suppliers)
            .Returns(fakeSuppliers.Object);

            mockTownService
            .Setup(ts => ts.Create(fakeTownName))
            .Callback(addingTownToTowns);

            addressServiceStub
            .Setup(addServ => addServ.Create(fakeAddressText, fakeTownName))
            .Callback(addingAddressToAddresses);

            // Act
            supplierService.AddSupplierRange(fakeSupplierImportModels);

            // Assert
            mockTownService.Verify(ts => ts.Create(fakeTownName), Times.Once);
        }
        public void Invoke_ContextSaveChanges_When_ValidationsPass()
        {
            // Arrange
            var fakeAddressText          = "testAddress";
            var fakeTownName             = "testTown";
            var fakeSupplierImportModel  = new Mock <ISuppliersImportModel>();
            var fakeSupplierImportModels = new List <ISuppliersImportModel>()
            {
                fakeSupplierImportModel.Object
            };

            var fakeTown = new Town()
            {
                Name = fakeTownName
            };
            var fakeTowns = new List <Town>()
            {
                fakeTown
            }.GetQueryableMockDbSet();

            var fakeAddress = new Address()
            {
                AddressText = fakeAddressText, Town = fakeTown
            };
            var fakeAddresses = new List <Address>()
            {
                fakeAddress
            }.GetQueryableMockDbSet();

            var fakeSupplier  = new Mock <Supplier>();
            var fakeSuppliers = new List <Supplier>()
            {
            }.GetQueryableMockDbSet();

            var mockCtx            = new Mock <IOnlineStoreContext>();
            var townServiceStub    = new Mock <ITownService>();
            var addressServiceStub = new Mock <IAddressService>();
            var mapperStub         = new Mock <IMapper>();

            var supplierService = new SupplierService(mockCtx.Object, addressServiceStub.Object, townServiceStub.Object, mapperStub.Object);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Address)
            .Returns(fakeAddressText);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Town)
            .Returns(fakeTownName);

            mapperStub
            .Setup(m => m.Map <ISuppliersImportModel, Supplier>(fakeSupplierImportModel.Object))
            .Returns(fakeSupplier.Object);

            mockCtx
            .Setup(ctx => ctx.Towns)
            .Returns(fakeTowns.Object);

            mockCtx
            .Setup(ctx => ctx.Addresses)
            .Returns(fakeAddresses.Object);

            mockCtx
            .Setup(ctx => ctx.Suppliers)
            .Returns(fakeSuppliers.Object);

            // Act
            supplierService.AddSupplierRange(fakeSupplierImportModels);

            // Assert
            mockCtx.Verify(ctx => ctx.SaveChanges(), Times.Once);
        }
        public void Invoke_AddMethod_ToAddSupplier_ToSuppliers_Once_PerSupplier()
        {
            // Arrange
            var fakeAddressText          = "testAddress";
            var fakeTownName             = "testTown";
            var fakeSupplierImportModel  = new Mock <ISuppliersImportModel>();
            var fakeSupplierImportModels = new List <ISuppliersImportModel>()
            {
                fakeSupplierImportModel.Object
            };

            var fakeTown = new Town()
            {
                Name = fakeTownName
            };
            var fakeTowns = new List <Town>()
            {
                fakeTown
            }.GetQueryableMockDbSet();

            var fakeAddress = new Address()
            {
                AddressText = fakeAddressText, Town = fakeTown
            };
            var fakeAddresses = new List <Address>()
            {
                fakeAddress
            }.GetQueryableMockDbSet();

            var fakeSupplier  = new Mock <Supplier>();
            var mockSuppliers = new List <Supplier>()
            {
            }.GetQueryableMockDbSet();

            var ctxStub            = new Mock <IOnlineStoreContext>();
            var townServiceStub    = new Mock <ITownService>();
            var addressServiceStub = new Mock <IAddressService>();
            var mapperStub         = new Mock <IMapper>();

            var supplierService = new SupplierService(ctxStub.Object, addressServiceStub.Object, townServiceStub.Object, mapperStub.Object);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Address)
            .Returns(fakeAddressText);

            fakeSupplierImportModel
            .SetupGet(sim => sim.Town)
            .Returns(fakeTownName);

            mapperStub
            .Setup(m => m.Map <ISuppliersImportModel, Supplier>(fakeSupplierImportModel.Object))
            .Returns(fakeSupplier.Object);

            ctxStub
            .Setup(ctx => ctx.Towns)
            .Returns(fakeTowns.Object);

            ctxStub
            .Setup(ctx => ctx.Addresses)
            .Returns(fakeAddresses.Object);

            ctxStub
            .Setup(ctx => ctx.Suppliers)
            .Returns(mockSuppliers.Object);

            // Act
            supplierService.AddSupplierRange(fakeSupplierImportModels);

            // Assert
            mockSuppliers.Verify(s => s.Add(It.IsAny <Supplier>()), Times.Once);
        }