Faking live instances |
Top Previous Next |
What happens if you want to change the behavior of the class you test? For example, the Person object is under test, and it has a method called GetAddress that goes to the database and brings back an Address object.
class Person { public: Address* FillAddress() {return Address::GetFromDatabase();} int GetHouseNumber() { FillAddress()->GetHouseNumber();} ... }
In our case we want to test that the GetHouseNumber method works and returns the default 1. But we don't want to access the database. To do this we can fake the behavior of the FillAddress method - in our case, to return a fake Address.
TEST_F(PersonTests, DefaultHouseNumberIs1) { Person* person = new Person();
WHEN_CALLED(person->FillAddress()).Return(new Address());
ASSERT_EQ(1, person->GetHouseNumber()); }
Note that we're creating the Person object with a regular new. It's a regular Person object. We then use the same WHEN_CALLED syntax, to ignore the FillAddress call. |
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.