duanlidi1051 2018-01-18 01:04
浏览 39
已采纳

如果DI不可行,如何将实例化与单元测试的实现分离?

I have the following code (simplified and details changed for this question):

    class model_to_be_tested {
        // an array that holds a collection of thing A
        public $array_of_thing_A;

        // already doing constructor injection for the data object
        public __construct($data_object) {
         // details here
        }

        public function add_new_thing_A($has_relationship) {
          $thing_A = new Thing_A();
          $thing_A->is_thing = true;
          $thing_A->has_relationship_with_thing_B = $has_relationship;

          if ($has_relationship) {
             $thing_B = new Thing_B();
             $thing_A->relationship_with = $thing_B;
          }

          $this->array_of_thing_A[] = $thing_A;
      }
    }

In the above example, I have to decouple the instantiation of Thing_A and Thing_B from the add_new_thing method. However, a simple constructor injection will not do for these two classes. This is because I need fresh instances of Thing_A and Thing_B every time add_new_thing is called so that Thing_A can be added to the array_of_thing_A.

How can I make this function unit testable? And more specifically for me to use mocks of Thing_A and Thing_B in testing this function in PHPUnit?

Any suggestions with code example will be appreciated.

Additionally, I would like to mention that Thing_A and Thing_B are used elsewhere in the codebase that I am working with and the code using these classes will eventually need to be unit tested. Solutions that are too localized and would cause repeated code elsewhere will not be too ideal in my situation. Thank you.

  • 写回答

2条回答 默认 最新

  • dongpi9494 2018-01-18 14:33
    关注

    As commenter xmike mentioned, you could use the factory pattern. You would inject a factory object through the ctor as well. Then you could have a factory that provides simplified instances of your Thing_A and Thing_B.

    class ThingFactory {
        public function buildThingA() {
            return new Thing_A(); // or MockThing_A if you go the ducktyping route
        }
    
        public function buildThingB() {
            return new Thing_B();
        }
    }
    
    class model_to_be_tested {
            // an array that holds a collection of thing A
            public $array_of_thing_A;
    
            // you could go the typed route and have an interface for this
            private $factory;
    
            // already doing constructor injection for the data object
            public __construct($data_object, $factory) {
             // details here
             $this->factory = $factory;
            }
    
            public function add_new_thing_A($has_relationship) {
              $thing_A = $this->factory->buildThingA();
              $thing_A->is_thing = true;
              $thing_A->has_relationship_with_thing_B = $has_relationship;
    
              if ($has_relationship) {
                 $thing_B = $this->factory->buildThingB();
                 $thing_A->relationship_with = $thing_B;
              }
    
              $this->array_of_thing_A[] = $thing_A;
          }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播