using System.Collections.Generic; using NUnit.Framework; using Rhino.Mocks; using TypeMock; namespace MockChaining { [TestFixture] public class MethodChainingTests { [Test] public void MethodChaining() { Customer customer = new Customer(); using (RecordExpectations r = RecorderManager.StartRecording()) { r.ExpectAndReturn(customer.Orders[0].OrderItems[0].Product.Color, "Red"); } Assert.AreSame("Red", customer.Orders[0].OrderItems[0].Product.Color); MockManager.Verify(); } [Test] public void MethodChaining_Rhino_virtual() { MockRepository mocks = new MockRepository(); Order order = mocks.DynamicMock(); OrderItem orderItem = mocks.DynamicMock(); Product product = mocks.DynamicMock(); Customer customer = new Customer(); customer.Orders.Add(order); using (mocks.Record()) { Expect.Call(order.OrderItems).Return(new OrderItem[] { orderItem }); Expect.Call(orderItem.Product).Return(product); Expect.Call(product.Color).Return("Red"); } using (mocks.Playback()) { Assert.AreSame("Red", customer.Orders[0].OrderItems[0].Product.Color); } } } public class Customer { private IList m_Orders = new List(); public IList Orders { get { return m_Orders; } set { m_Orders = value; } } } public class Order { private IList m_OrderItems = new List(); public virtual IList OrderItems { // Virtual erfoderlich wg. "Proxy Mocking" get { return m_OrderItems; } set { m_OrderItems = value; } } } public class OrderItem { public virtual Product Product { get; set; } // Virtual erfoderlich wg. "Proxy Mocking" } public class Product { public virtual string Color { get; set; } // Virtual erfoderlich wg. "Proxy Mocking" } }