1
1230987za
Hi,
Let's say I have the following class:
class foo {
public:
foo();
void addItem(int item);
private:
int* items;
};
I want to write a unit test program to test foo::addItem(), to do that
the test program needs to know the contents of foo::items.
So I can add one more public function to class foo:
int* getItems();
But suppose in my case, the client code of class foo does not need
getItems() at all, this function serves only for test in this case.
So I am considering to have a separate class fooTest and this class is
a friend class of class foo so that it has an API to retrieve all
private data of foo.
I believe this works, but I am wondering if there is better solution.
Basically my goals are:
1. I do not want the class to be bloated with a lot of API which only
serve for test purpose
2. I want the function class to be independent from test class
Thanks a lot.
Let's say I have the following class:
class foo {
public:
foo();
void addItem(int item);
private:
int* items;
};
I want to write a unit test program to test foo::addItem(), to do that
the test program needs to know the contents of foo::items.
So I can add one more public function to class foo:
int* getItems();
But suppose in my case, the client code of class foo does not need
getItems() at all, this function serves only for test in this case.
So I am considering to have a separate class fooTest and this class is
a friend class of class foo so that it has an API to retrieve all
private data of foo.
I believe this works, but I am wondering if there is better solution.
Basically my goals are:
1. I do not want the class to be bloated with a lot of API which only
serve for test purpose
2. I want the function class to be independent from test class
Thanks a lot.