mercredi 29 avril 2020

Mocking classes with template methods

Is there any pattern for testing classes that use classes containing template methods in public api? I know that in dynamic polymorphism mocking interface is the solution like this:

struct Interface {
 virtual void foo() = 0; 
 virtual ~Interface() = default;
};

class TestedClass {
public:
  TestedClass(Interface& i) {}
  // ... rest of the class
};

struct IMock : public Interface {
 void foo() override {} 
};

void test() {
  IMock bar;
  TestedClass baz(bar);
}

But what can I do with something like below? Is there an idiomatic way to test this?

struct Interface {
 template<class T>
 void foo() {
   // do stuff depending on type
 }
};

class TestedClass {
public:
  TestedClass(Interface& i) {}
  // ... rest of the class
  // uses Interface foo with multiple types
};

Aucun commentaire:

Enregistrer un commentaire