7. Nightly Functional Tests via custom attribute
[TestMethod, ExecuteNightly]
public void FacebookSearch()
{
var adapter = new FacebookAdapter();
var places = adapter.Search(coords, 10000);
Assert.IsTrue(places.Count() > 0);
}
7
8. Hard to test code that uses statics
public Configuration GetConfiguration()
{
string[] conf = File.ReadAllLines("Config.xml");
// parse contents and construct Configuration obj
¡
8
9. Constructors as like static methods
Keep them simple
public class User
{
public User(int userId)
{
DataProvider provider = new DataProvider();
var data = provider.GetUserInfo(userId);
¡
}
9
10. Hard to test code that has object
initialization inside business logic
public Deal[] GetGrouponDeals()
{
WebClient client = new WebClient();
// Logic to retrieve and parse the response
¡
}
10
11. Separate object graph from logic
Ie. remove new operators
public GrouponAdapter(ILooptWebClient webClient)
{
_webClient = webClient;
}
public Deal[] GetGrouponDeals()
{
// Use _webClient to retrieve URL
¡
11
12. Dependency injection ? Test able code
[TestMethod]
public void VerifyDeals()
{
var testClient = new TestWebClient();
var groupon = new GrouponAdapter(testClient);
// Now you control the URL response
¡
}
12
13. Root of application will have logic to
construct object graphs
public CoreService(
IDealManager dealManager,
IUserManager userManager,
¡
public DealManager(
IGrouponAdapter grouponAdapter,
IDataProvider dataProvider,
IPoiController poiController)
13
14. Use ¡°Ninject¡± to construct object graph
¡
_kernel.Get<GrouponAdapter>();
¡
14
15. Use ¡°Ninject¡± to construct object graph
¡
Bind<ILooptWebClient>().To<LooptWebClient>();
¡
15
16. ¡°Moq¡± as object mock framework
[TestMethod]
public void VerifyDeals()
{
var testClient = new Mock<ILooptWebClient>();
testClient.
Setup(c => c.GetUrl(It.IsAny<Uri>())).
Returns(Resources.MyResponse);
var groupon = new GrouponAdapter(testClient);
¡
}
16