Пример #1
0
        public void CreateSavesNewProductOption()
        {
            var name        = "Test Name";
            var description = "Test Description";

            var opt1 = new Mock <IProductOption>(MockBehavior.Strict);

            opt1.SetupGet(p => p.Id).Returns(_id1);
            opt1.SetupGet(p => p.ProductId).Returns(_id2); //  Yes not _prodId
            opt1.SetupGet(p => p.Name).Returns(name);
            opt1.SetupGet(p => p.Description).Returns(description);

            IProductOption opt2 = null;

            _repo.Setup(r => r.Get(_id1)).Returns <IProductOption>(null);
            _repo.Setup(r => r.Save(It.IsAny <IProductOption>(), true)).Verifiable();
            _repo.Setup(r => r.Save(It.IsAny <IProductOption>(), false)).Callback <IProductOption, bool>((o, _) => opt2 = o);

            _service.Create(_prodId, opt1.Object);

            _repo.Verify(r => r.Get(_id1), Times.Once);
            _repo.Verify(r => r.Save(It.IsAny <IProductOption>(), true), Times.Never);
            _repo.Verify(r => r.Save(It.IsAny <IProductOption>(), false), Times.Once);

            opt2.Id.Should().Be(_id1);
            opt2.ProductId.Should().Be(_prodId);
            opt2.Name.Should().Be(name);
            opt2.Description.Should().Be(description);
        }
        public async Task <IHttpActionResult> Post(Guid productId, [FromBody] ProductOptionsDto prodOptDto)
        {
            await _service.Create(productId, _mapper.Map <ProductOption>(prodOptDto));

            return(Ok());
        }