Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zainulabidin302/8266e13fe214deefe9d263c53e833a6a to your computer and use it in GitHub Desktop.
Save zainulabidin302/8266e13fe214deefe9d263c53e833a6a to your computer and use it in GitHub Desktop.
Unit test and dependency Injection Example
class Device { };
class Tablet : public Device{};
class Iphone : public Device{};
class Mobile : public Device{};
class Person {
//Iphone device; rahter then concrete implementation we are using interface
Device * device;
public:
Person(Device * device) {
this->device = device;
}
/*Person(Iphone device) {
this->device = device;
}*/
void setDevice(Device * device) {
this->device = device;
}
};
class MockMobile : public Device{};
class PersonTest {
void canAPersonAffordEveryMobile() {
Person *p1 = new Person(new Tablet());
Person *p2 = new Person(new Iphone());
Person *p3 = new Person(new Mobile());
// We can check multiple configuration
// or even a mock configration
Person *p10 = new Person(0);
p10->setDevice(new MockMobile());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment