Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created November 11, 2013 08:22
Show Gist options
  • Save hatelove/7409682 to your computer and use it in GitHub Desktop.
Save hatelove/7409682 to your computer and use it in GitHub Desktop.
需要用 mock 測試的 sample
using System;
using System.Collections.Generic;
namespace MessageHandler.Test
{
public class MyService
{
private ICustomerDao _customerDao;
public MyService(ICustomerDao customerDao)
{
this._customerDao = customerDao;
}
// 該如何測試 MyMethod?
public void MyMethod()
{
var customers = GetCustomers();
foreach (var customer in customers)
{
if (customer.IsMale)
{
this._customerDao.Insert(customer);
}
}
}
private IEnumerable<Customer> GetCustomers()
{
yield return new Customer { Id = 1, IsMale = true };
yield return new Customer { Id = 2, IsMale = false };
yield return new Customer { Id = 3, IsMale = true };
yield return new Customer { Id = 4, IsMale = false };
yield return new Customer { Id = 5, IsMale = true };
}
}
public class Customer
{
public int Id { get; set; }
public bool IsMale { get; set; }
}
public interface ICustomerDao
{
void Insert(Customer customer);
}
public class CustomerDao : ICustomerDao
{
public void Insert(Customer customer)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment