unit testing - c++ dependency injection to test a class that class system calls -
i trying use template dependency injection test c++ class uses c system calls operate on file descriptor. ideia have abstract class , instance wrap system calls read(), write(), etc. use mock test target class. abstract class , system calls looks like: (i going omit parameters clear)
class oscall{ read()=0; write()=0 }; class defaultoscall : public oscall{ read(){...} write(){...} } later inject oscall in class want use it:
template<typename oscall> class fd{ public: oscall oscall_; oscall &getoscall(){return oscall_;} read(){oscall_.read()} write(){oscall_.write()} } now if want use mock test fd class need pass mock in template paramenter , mock instance using getoscall.
let's want use fd member of class:
template<typename oscall> class user{ public: dosomething(){fd_.read();.......} oscall &getmemberoscall(){return fd_.getoscall()} private: fd<oscall> fd_; } if want test user mock, can oscall instance using getmemberoscall, works, 1 of best ways it? in end want inject mock class member , expect return values of member's mock. hope made myself clear.
thanks
i recommend rename getmemberoscall() getoscall().
this seem enable used in conjunction generic templates template classes need implement getoscall() method, , able instantiate template either fd<oscall>, or user<oscall>. seems bit more flexible approach.
Comments
Post a Comment