Last active
July 20, 2016 21:22
-
-
Save zainulabidin302/8266e13fe214deefe9d263c53e833a6a to your computer and use it in GitHub Desktop.
Unit test and dependency Injection Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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