Mocking C++ classes with dependency injection

后端 未结 3 1791
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 04:20

Say you\'re testing class A and it has a dependency injection of B which has a dependency injection of C.
So you mock B

3条回答
  •  無奈伤痛
    2021-01-15 04:30

    In this case you should inject by pointer and not by reference, then you could pass a NULL pointer. This would work assuming you're object is indeed a mock and not a fake object, therefore it has no real dependency on the injected object.

    For boost::shared_ptr you could do the following:

    boost::shared_ptr null_c_ptr;
    MockB mock_b(null_c_ptr);
    

提交回复
热议问题