I'd like to test that the function client calls constructor of Target and then calls Target::foo() correctly. I was trying to write the following code:
#define BOOST_AUTO_TEST_MAIN #include <boost/test/auto_unit_test.hpp> #include <turtle/mock.hpp> MOCK_CLASS( Target ) { MOCK_CONSTRUCTOR(Target, 0, (), Target_def) MOCK_METHOD(foo, 0, void()) }; void client() { Target t; t.foo(); } BOOST_AUTO_TEST_CASE( test1 ) { MOCK_EXPECT(Target::Target_def).once(); MOCK_EXPECT( /* How to write? */.foo).once(); client(); }
But I cannot get the object that is constructed in the client(), so I cannot write an appropriate identifier in MOCK_EXCEPT.
How should I write?
Hi Takatoshi,
There is no means to directly set a "static" expectation, however this is a planned feature because it has been requested a few times already, see for instance https://sourceforge.net/p/turtle/tickets/21/
In the meanwhile here is a possible workaround :
Does this help ?
MAT.
Hi MAT,
Thank you for your reply. But the workaround that you suggested is something different from what I want to do. I should write Target class.
The member function foo() is not static. I'd like to test that foo() is called from the function client() once with the object constructed as t.
I wrote another example that describes my intention clearer.
Well, foo is not static either in the code I posted, it is foo_s which is static, but its identifier is foo, sorry for the confusion.
Anyway here is what I would probably do for your second use case :
Does it make sense ?
MAT.
It's perfectly make sense. Thank you very much! I didn't have an idea that I can write MOCK_EXPECT in calls as a parameter. It's a kind of lazy evaluation. It is very helpful.
By the way, I have two other issues relate to this ticket and I solved it by myself based on your advice. Let me explain:
I separate the mock class' copy constructor with two parts. One is parameter checker, the other is invoker with copied object.
Ah yes C++11 lambdas can be used as well, good point !
I personally would probably not have split in two different expectations but have done the following :
Of course your solution works well too !
MAT.
Ah! Your solution is more sophisticated. I like it. Thank you.