public void Test_Charge_Customer_Count() { //arrange // 透過 MockRepository.GenerateStub<T>() // 來建立某一個 T 型別的 stub object // 以例子來說,是建立 ICheckInFee 介面的實作子類。 ICheckInFee stubCheckInFee = MockRepository.GenerateStub<ICheckInFee>(); // 把該 stub object 透過建構式,設定給測試目標物件。 Pub target = new Pub(stubCheckInFee); // 定義當呼叫到該 stub object 的哪一個方法時,若傳入的參數為何,則 stub 要回傳什麼。 stubCheckInFee.Stub(x => x.GetFee(Arg<Customer>.Is.Anything)).Return(100); var customers = new List<Customer> { new Customer{ IsMale=true}, new Customer{ IsMale=false}, new Customer{ IsMale=false}, }; decimal expected = 1; //act var actual = target.CheckInFriday(customers); //assert Assert.AreEqual(expected, actual); }
public void Test_Friday_Charge_Customer_Count() { // 在 using (ShimsContext.Create()){} 的範圍中,會使用 Fake 組件。 using (ShimsContext.Create()) { System.Fakes.ShimDateTime.TodayGet = () => { //2012/10/19為Friday return new DateTime(2015, 08, 07); }; //arrange ICheckInFee stubCheckInFee = MockRepository.GenerateStub<ICheckInFee>(); Pub target = new Pub(stubCheckInFee); stubCheckInFee.Stub(x => x.GetFee(Arg<Customer>.Is.Anything)).Return(100); var customers = new List<Customer> { new Customer{ IsMale=true}, new Customer{ IsMale=false}, new Customer{ IsMale=false}, }; decimal expected = 1; //act var actual = target.CheckInFriday(customers); //assert Assert.AreEqual(expected, actual); } }
public void Test_CheckIn_Charge_Only_Male() { //arrange mock var customers = new List<Customer>(); //2男1女 var customer1 = new Customer { IsMale = true }; var customer2 = new Customer { IsMale = true }; var customer3 = new Customer { IsMale = false }; customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); MockRepository mock = new MockRepository(); ICheckInFee stubCheckInFee = mock.StrictMock<ICheckInFee>(); using (mock.Record()) { //期望呼叫ICheckInFee的GetFee()次數為2次 stubCheckInFee.GetFee(customer1); LastCall .IgnoreArguments() .Return((decimal)100) .Repeat.Times(2); } using (mock.Playback()) { var target = new Pub(stubCheckInFee); var count = target.CheckInFriday(customers); } }
public void Test_Income() { //arrange ICheckInFee stubCheckInFee = MockRepository.GenerateStub<ICheckInFee>(); Pub target = new Pub(stubCheckInFee); stubCheckInFee.Stub(x => x.GetFee(Arg<Customer>.Is.Anything)).Return(100); var customers = new List<Customer> { new Customer{ IsMale=true}, new Customer{ IsMale=false}, new Customer{ IsMale=false}, }; var inComeBeforeCheckIn = target.GetInCome(); Assert.AreEqual(0, inComeBeforeCheckIn); decimal expectedIncome = 100; //act var chargeCustomerCount = target.CheckInFriday(customers); var actualIncome = target.GetInCome(); //assert Assert.AreEqual(expectedIncome, actualIncome); }