Пример #1
0
        public void Order_Save_Update_with_changed_line_items_calls_expected_repository_methods()
        {
            var mocks = new MockRepository();

            using (mocks.Record())
            {
                var oldTimestamp = new byte[8];
                var newTimestamp = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 };
                //mock DTO's
                var lineItemDto1 = mocks.StrictMock <ILineItemDto>();
                Expect.Call(lineItemDto1.Id).Return(200).Repeat.Any();
                Expect.Call(lineItemDto1.ProductName).Return("Widget A").Repeat.Any();
                Expect.Call(lineItemDto1.Price).Return(10M).Repeat.Any();
                Expect.Call(lineItemDto1.Quantity).Return(2).Repeat.Any();
                Expect.Call(lineItemDto1.Timestamp).Return(oldTimestamp).Repeat.Any();
                var lineItemDto2 = mocks.StrictMock <ILineItemDto>();
                Expect.Call(lineItemDto2.Id).Return(201).Repeat.Any();
                Expect.Call(lineItemDto2.ProductName).Return("Widget B").Repeat.Any();
                Expect.Call(lineItemDto2.Price).Return(20.5M).Repeat.Any();
                Expect.Call(lineItemDto2.Quantity).Return(3).Repeat.Any();
                Expect.Call(lineItemDto2.Timestamp).Return(oldTimestamp).Repeat.Any();
                var orderDto1 = mocks.StrictMock <IOrderDto>();
                Expect.Call(orderDto1.Id).Return(100).Repeat.Any();
                Expect.Call(orderDto1.Customer).Return("Bob").Repeat.Any();
                Expect.Call(orderDto1.Date).Return(DateTime.Parse("1/1/2008")).Repeat.Any();
                Expect.Call(orderDto1.ShippingCost).Return(5.5M).Repeat.Any();
                Expect.Call(orderDto1.Timestamp).Return(oldTimestamp).Repeat.Any();
                Expect.Call(orderDto1.LineItems).Return(new[] { lineItemDto1, lineItemDto2 }).Repeat.Any();
                var updatedLineItemDto2 = mocks.StrictMock <ILineItemDto>();
                updatedLineItemDto2.Id          = 200;
                updatedLineItemDto2.OrderId     = 100;
                updatedLineItemDto2.ProductName = "Widget A+";
                updatedLineItemDto2.Price       = 10M;
                updatedLineItemDto2.Quantity    = 2;
                updatedLineItemDto2.Timestamp   = oldTimestamp;
                Expect.Call(updatedLineItemDto2.Timestamp).Return(newTimestamp);
                var instertedLineItemDto3 = mocks.StrictMock <ILineItemDto>();
                instertedLineItemDto3.OrderId     = 100;
                instertedLineItemDto3.ProductName = "Widget C";
                instertedLineItemDto3.Price       = 15.1M;
                instertedLineItemDto3.Quantity    = 4;
                Expect.Call(instertedLineItemDto3.Id).Return(202);
                Expect.Call(instertedLineItemDto3.Timestamp).Return(newTimestamp);
                //mock read context
                IOrderContext readContext = mocks.StrictMock <IOrderContext>();
                Expect.Call(readContext.FetchSingleWithLineItems(100)).Return(orderDto1);
                readContext.Dispose();
                //mock transactional context
                IOrderContext trxContext = mocks.StrictMock <IOrderContext>();
                trxContext.UpdateLineItem(updatedLineItemDto2);
                trxContext.DeleteLineItem(201, 100);
                trxContext.InsertLineItem(instertedLineItemDto3);
                trxContext.CompleteTransaction();
                trxContext.Dispose();
                //mock repository
                var repository = base.MockRepository <IOrderContext>(mocks);
                Expect.Call(repository.CreateContext(false)).Return(readContext);
                Expect.Call(repository.CreateContext(true)).Return(trxContext);
                Expect.Call(repository.CreateLineItemDto()).Return(updatedLineItemDto2);
                Expect.Call(repository.CreateLineItemDto()).Return(instertedLineItemDto3);
            }
            using (mocks.Playback())
            {
                //fetch existing order
                IOrderFactory factory = new OrderFactory();
                var           entity  = factory.Fetch(100);
                ILineItem     child;
                //change line item 1
                child             = entity.LineItems[0];
                child.ProductName = "Widget A+";
                //delete line item 2
                entity.LineItems.RemoveAt(1);
                //add new line item 3
                child             = entity.LineItems.AddNew();
                child.ProductName = "Widget C";
                child.Price       = 15.1M;
                child.Quantity    = 4;
                //test that entity is old and dirty
                Assert.That(entity.IsNew, Is.False);
                Assert.That(entity.IsDirty, Is.True);
                //perform update
                entity = entity.Save();
                //test that entity is old and clean
                Assert.That(entity.IsNew, Is.False);
                Assert.That(entity.IsDirty, Is.False);
            }
        }
Пример #2
0
 private void Child_DeleteSelf(IOrder parent, IOrderContext context)
 {
     //delete line item data
     context.DeleteLineItem(_id, parent.Id);
 }