Break Dependencies · Los Techies
12 August, 2009. It was a Wednesday.
Today’s refactoring is useful if you are trying to introduce unit tests into your code base as testing “seams” are needed to properly mock/isolate areas you don’t wish to test. In this example we have client code that is using a static class to accomplish some work. The problem with this when it comes to unit testing because there is no way to mock the static class from our unit test. To work around this you can apply a wrapper interface around the static to create a seam and break the dependency on the static.
1: public class AnimalFeedingService
2: {
3: private bool FoodBowlEmpty { get; set; }
4:
5: public void Feed()
6: {
7: if (FoodBowlEmpty)
8: Feeder.ReplenishFood();
9:
10: // more code to feed the animal
11: }
12: }
13:
14: public static class Feeder
15: {
16: public static void ReplenishFood()
17: {
18: // fill up bowl
19: }
20: }
All we did to apply this refactoring was introduce an interface and class that simply calls the underlying static class. So the behavior is still the same, just the manner in which it is invoked has changed. This is good to get a starting point to begin refactoring from and an easy way to add unit tests to your code base.
1: public class AnimalFeedingService
2: {
3: public IFeederService FeederService { get; set; }
4:
5: public AnimalFeedingService(IFeederService feederService)
6: {
7: FeederService = feederService;
8: }
9:
10: private bool FoodBowlEmpty { get; set; }
11:
12: public void Feed()
13: {
14: if (FoodBowlEmpty)
15: FeederService.ReplenishFood();
16:
17: // more code to feed the animal
18: }
19: }
20:
21: public interface IFeederService
22: {
23: void ReplenishFood();
24: }
25:
26: public class FeederService : IFeederService
27: {
28: public void ReplenishFood()
29: {
30: Feeder.ReplenishFood();
31: }
32: }
33:
34: public static class Feeder
35: {
36: public static void ReplenishFood()
37: {
38: // fill up bowl
39: }
40: }
We can now mock IFeederService during our unit test via the AnimalFeedingService constructor by passing in a mock of IFeederService. Later we can move the code in the static into FeederService and delete the static class completely once we have some tests in place.
This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.
相关知识
Break Dependencies · Los Techies
los免抠元素
Remove Middle Man · Los Techies
The Ultimate Guide to Getting Started with the Play Framework
编程中遇到的问题break ,continue,find,imbinarize,laplace提取图像轮廓线
Dia de los Muertos 发现差异矢量迷宫游戏与跳舞的骷髅。背景图片免费下载
两个json数组进行比较判断是否重复,重复break ,否则继续执行
1+x移动应用开发(中级) 账号服务集成 推送服务 定位服务 机器学习服务(文本识别)
android 支付功能集成,实战:使用聚易付移动支付服务(MixpayAndroidSDK)快速集成移动支付功能...
Urban Green Infrastructure
网址: Break Dependencies · Los Techies https://www.huajiangbk.com/newsview2270604.html
上一篇: Languages |
下一篇: Front Matter |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039