We all hear how unit tests can ensure higher quality code and help us in day to day refactoring, but is it feasible to write and maintain unit tests in a fast paced startup company?
This is a presentation by server lead, Heine Frifeldt, on how unit tests was gradually introduced into the server code base at Loopt, which tools are used in the continuous build environment, coding techniques and lessons learned.
78. Example Class using Dependency Injection public class GrouponAdapter : IGrouponAdapter { private readonly IPoiController _poiController; private readonly DataContextProvider _contextProvider; private readonly ILooptWebClient _looptWebClient; [Inject] public GrouponAdapter(IPoiController poiController, DataContextProvider contextProvider, ILooptWebClient looptWebClient) { _poiController = poiController; _contextProvider = contextProvider; _looptWebClient = looptWebClient; } … 17
79. Example Ninject Bindings public class LooptLogicModule : NinjectModule { public override void Load() { Bind<IPoiController>().To<PoiController>().InSingletonScope(); Bind<ILooptWebClient>().To<LooptWebClient>().InSingletonScope(); … private IKernel _kernel; _kernel.Get<IGrouponAdapter>(); 18
80. Example Unit Test using Moq [TestMethod] public void ParseGrouponDeals() { // Results in 6 deals being returned. var webClient = new Mock<ILooptWebClient>(); webClient.Setup(c => c.DownloadString(It.IsAny<Uri>())). Returns(Resources.Groupon_Deals_In_Austin); IGrouponAdapter ga = new GrouponAdapter(null, webClient.Object); Deal[] deals = ga.GrouponDeals(new Coordinate(30.44595, -97.79016)); Assert.AreEqual(6, deals.Length, "Expected 6 deals"); } 19
No constructorStatic method to execute logicMakes connection to DB and invokes stored procReads from binary stream and converts to storecproc arguments
Notice ConfigurationManager.GetSection is a static from BCLSome code paths would check “IsProduction” config setting and behave according to environment