I am trying to use template dependency injection to test a C++ class that uses C system calls to operate over a file descriptor. The ideia is to have an abstract class and an instance to wrap the system calls like read(), write(), etc. Then I use a mock to test my target class. The abstract class and system calls looks like: (I am going to omit parameters to be clear)
class OSCall{
read()=0;
write()=0
};
class DefaultOSCall : public OSCall{
read(){...}
write(){...}
}
Later I inject the OSCall in the class I want to use it:
template<typename OSCall>
class FD{
public:
OSCall osCall_;
OSCall &GetOSCall(){return osCall_;}
read(){osCall_.read()}
write(){osCall_.write()}
}
Now if I want to use a mock to test my FD class I just need to pass my mock in the template paramenter and get the mock instance using GetOsCall.
Let's say I want to use the FD as a member of another class:
template<typename OSCall>
class User{
public:
DoSomething(){fd_.read();.......}
OSCall &GetMemberOSCall(){return fd_.GetOSCall()}
private:
FD<OSCall> fd_;
}
If I want to test the user with a mock, I can get the OSCall instance using the GetMemberOSCall, it works, but is it one of the best ways to do it? In the end I want to inject a mock to a class member and expect the return values of the member's mock. I hope I made myself clear.
Thanks
Aucun commentaire:
Enregistrer un commentaire