chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
Is there a way to pass the arguments of a faked method call on to the faked version?

Let\\\'s say I have -

class Factory  {

  public:

    int Build(int argument) { return argument; }

  };

 Factory * fake = FAKE<Factory>();

WHEN_CALLED(fake->Build()).DoStaticOrGlobalInstead(<some version that accepts the incoming argument>);

so that when I call fake->Build(10), I can act on the \\\"10\\\" instead of always returning the same value.

Ideally, what I want to do is something like this (obviously this is a contrived example, the real situation is more complex):

WHEN_CALLED(fake->Build()).DoStaticOrGlobalInstead([](int x) { return x*x; });

This functionality - a one-line mocked version of the original class - seems to me sort of central to the concept of mocking - so am I missing something obvious on how to use Typemock to accomplish this?

Thanks.
asked by (1.1k points)

1 Answer

0 votes

Hey beauchai, 

I am not sure I understood correctly. What do you mean when you say \"version\"? (\"Is there a way to pass the arguments of a faked method call on to the faked version?\")

In case you are trying to use DoInstead behavior, you can read it here:

 http://www.typemock.com/docs/?book=Ipp&page=doinstead_behavior.htm

Conditional behavior: 

http://www.typemock.com/docs/?book=Ipp&page=conditional_behavior_faking.htm

If that\'s not what you meant and/or you\'ve already tried, please explain in short what are you trying to do and what do you mean by \"version\".

Cheers

answered by (620 points)
So here is the simplest case I can use to explain the request:

class Foo

  {

  public:

    int bar(int i) { return i; }

  };

  Foo *f = FAKE<Foo>();

 WHEN_CALLED(f->bar(_)).Return(<an expression derived from the actual value of \"_\" here>);

I want the mock result to depend on the VALUE of the argument that was passed to the member function when the test was run.  I don\'t see any examples where the Return() statement has any means to use the function arguments that were supplied in the test to construct the returned data or object.

Let\'s say I want my FAKE() to return i + 1 instead of i as in the original object.  Is there any reasonable way to accomplish this?
...