Sunday 15 March 2015

c++ - Google mock ByRef method -


I have a class that takes a boolean as a reference parameter and gives an integer: < Pre> square fu {public: bar my_bar; Virtual difference myMethod (bool & my_boolean) = 0; } /*...*/Inte Foo :: myMethod (bull & my_boolean) {if (my_bar == faucet) {my_boolean = false; Return -1; } And (my_boolean = true; return 0;}}

and I made a fake for this class:

  class mockfu: public fu {MOCK_METHOD1 (MyMethod, int (bool & my_boolean))}}   

I'm having problems setting expectations for this type of function because I return value and Specify the parameters to make my unit tests properly. How do I deal with this type of function with gmock After I thought what I thought, then I tried to solve the documentation:

 using : test :: setArgpontai; class mockmutter: public mutator {public: MOCK_METHOD2 (mutate , Blank (boolean mutate, int * value)); ...}; ... MockMutator mutator; EXPECT_CALL (mutator, mutate (true, _) .WillOnce (SetArgPointee <1> (5));   

But either I did not understand this example or it was not applicable for this matter. That are disposed of before the event?

Thanks in advance.

It's hard to get your question! Google mocks 'cook book' samples are so good.

Do you want to reuse the implementation for Foo: myMethod () , with the duplicate class, or what you do, only for specific call states Want to duplicate the effects (changed by return value and ref parameter)?

Usually this means that your FU class is to be changed / tailored, nor should it be directly inherited or behaved, do not know that the way you Define this 'default' behavior for the pure method, but it is doubtful that. You can leave only = 0 A better approach is to separate a real interface announcement such as:

  struct IFoo {virtual inte myMethod (bool & my_boolean) = 0 ; Virtual ~ IFoo () {}}; Square Fu: Public IFoo {// ...}; Class Mockfoo: Public IFoo {MOCK_METHOD1 (myMethod, int (bool & my_boolean)); };   

If you have a second case, then you have test :: return (value) and test :: setArgreefree < N & gt; (Value) (it has been found in very useful)

You should expect the call to look like this:

  Mokfu Fu; // Expect calls to your Mathith () Return-1 and set the correct EXPECT_CALL (foo, myMethod (_)) by the RIF object. Willon (DoAll (SetArgReferee & lt; 0> (Truth), Return (-1)); Expect to call // myMethod () 0 Return and set the false EXPECT_CALL (foo, myMethod (_)) with the ref argument. Willon (DoAll (SetArgReferee <0> (wrong), returns (0)));   

If you really want to reuse your original classes logic for myMethod () , then see again.

No comments:

Post a Comment