Faking members and local variables |
Top Previous Next |
When you have object members in your class that are created within the class and you need to set their behavior you can use FAKE_ALL API as well.
Let's look at a similar code sample but this time the address member is not a pointer.
class Person { private: Address address; public: Person() {} char* GetCity() {return address.GetCity();}
}
As before, we create a fake Address handle, and we can set behavior for all current Address object. The test still looks the same:
TEST_F(PersonTests, FakeAddress) { Address* fakeAddress = FAKE_ALL<Address>(); Person person;
WHEN_CALLED(fakeAddress->GetCity()).Return("NYC");
ASSERT_EQ("NYC", person.GetCity()); }
Similarly, if your method under-test creates a local variable during its run, use FAKE_ALL API to set its behavior:
int Person::DoSomething() { SomeClass someClass; return someClass.DoSomethingElse(); }
And the test set up might look like this:
TEST_F(PersonTests, FakeAddress) { Address* fakeClass = FAKE_ALL<SomeClass>(); Person person;
WHEN_CALLED(fakeClass->DoSomethingElse()).Return(1);
ASSERT_EQ(1, person.DoSomething()); }
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.