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 https://mxgxt.com/news/view/1718483
相关内容
Extract Subclass · Los TechiesRemove Middle Man · Los Techies
Pull Up Method · Los Techies
Replace conditional with Polymorphism · Los Techies
Rename (method, class, parameter) · Los Techies
TWICE新单《ENEMY》MV 预告片BREAK释出
课间休息 study break
苏新皓monster舞台dance break爆发力十足
忘了还有一段很喜欢的李河民dance break
硅谷明星公司News Break获1.15亿美元C轮融资,IDG资本投资
随便看看
- 杨紫琼到访白宫获拜登授勋,疑似被喊错姓名,超紧张面部表情不断
- 果香四溢:哎,最近有个事儿挺让人瞩目的。咱们中国留学生吴啸雷在美国碰上了点官司,这不,拜登总统9月12号就给签了特赦令。短短几天,人家9月16号就飞回国。这速度快得让人咋舌!这背后肯定是有国家级别的大动作。 你看,美国这么干,多半是为了和缓中美之间的关系波动。有专家说,这可能是一种外交上的妥协手段,因为两国关系一直都有些小摩擦。据CNN报道,中方此前也多次表达过对吴啸雷案子的关注,所以这次放人的...
- 从情妇到副总统,号称美国政坛“黑寡妇”,哈里斯的权力之路
- 杨幂曾考583入北影,和教授有亲戚关系,清华学子期末考试都拜杨幂!
- 杨幂穿旗袍撑出3D感 果然美貌和身材二选一问题在这

