method called several times abd check once with specific parameters
I need MyApplication to be constructed completely although I use Mock_MyApplication as a child class.
MOCK_BASE_CLASS
Please kindly let me know if my question/sample is not very clear. I am keen to use the tool in my important and urgent project right now.
How to mock some functions in my class
Can't seem to figure out why the mock method is just calling the base class method. Some help would be much appreciated.
MOCK_METHOD calling base class methods instead of mocked method.
Thanks Mathieu for the clarification. It works now as expected with both delete process amd MOCK_VERIFY. Cheers, Gianni
Hi Gianni, A mock object can either fail directly when a call is made that’s invalid, or if no call happens it must be «verified» to know all calls are done and to check if any unanswered call is remaining. The verification is either done manually (using MOCK_VERIFY for instance) or automatically when the mock object is destroyed. Here you are doing neither given you’re creating a naked pointer and never delete it. Does that make sense? MAT.
MOCK_EXPECT succedes when it should fail
Mathieu, Thanks. To be honest, I don't know how BOOST_REQUIRE behaves in a thread. BOOST_FAIL does the same thing as abort() though. Thanks again for your help. John
I think you probably have analyzed the problem correcty, but short of changing the policy I don’t think there is a way out of it. Out of curiosity how does BOOST_REQUIRE behave when it fails in a thread, as it’s what abort in the default policy is modelled after if I remember correctly?
Matthieu, Thanks for taking the time to reply, but that's not the issue. The code I posted was like that because the original version, that used std::shared_ptr<mockpanel> panel = std::make_shared<mockpanel>(), did the same thing.</mockpanel></mockpanel> I've come to the conclusion that the problem is that, when abort() is called due to the call sequence error, it's generating an exception in the thread that's created, which isn't being propagated to the main thread in a way that the Boost Test runtime...
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<mock::exception>'
You’re destroying MockPanel object twice, you should probably use std::make_shared to instantiate a shared pointer instead of MockPanel mockPanel; std::shared_ptr<mockpanel> panel { &mockPanel };
You’re destroying the shared pointer twice, you should probably use std::make_shared to instantiate a shared pointer instead of MockPanel mockPanel; std::shared_ptr<mockpanel> panel { &mockPanel };
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<mock::exception>'
Hi Gianni, Sorry I don’t have access to a C++ compiler at the moment so cannot verify this. What happens if you only set the first two expectations MOCK_EXPECT(macAddressFinderMock->find).with(mock::any, "192.168.0.223").never(); MOCK_EXPECT(macAddressFinderMock->find).with(mock::any, "192.168.0.222").returns("AA-BB-CC-DD-EE-FF"); and call it with "192.168.0.223"? MAT.
help with MOCK_EXPECT rules
Warning std::auto_ptr is deprecated
Turtle 1.3.1 has been released !
test succeed with one() or never() indifferently
Hi, Sorry for some reason I didn't get a notification when you created this ticket… The expectations are verified either when calling verify() explicitely on a mock object or automatically when the mock object is destroyed. Here you never delete the pointer… MAT.
test succeed with one() or never() indifferently
test succeed with one() or never() indifferently
Expecting a call with parameter + defining other calls with any
MOCK_BASE_CLASS inside BOOST_AUTO_TEST_CASE
Hi Gianni, This is a C++ limitation, see for instance https://stackoverflow.com/questions/5751977/local-type-as-template-arguments-in-c. Are you building with C++11 (or greater) enabled? Cheers, MAT.
MOCK_BASE_CLASS inside BOOST_AUTO_TEST_CASE
Thank you very much Mathieu, now it works. Gianni
Expecting a call with parameter + defining other calls with any
Hi Gianni, Have you tried MOCK_EXPECT(readerMock->beginGroup).once().with("hosts"); MOCK_EXPECT(readerMock->beginGroup); ? The expectations are tested in the order you set them, so if you start with MOCK_EXPECT(readerMock->beginGroup).at_least(1); as it doesn't have any with, it will «absorb» all calls preventing your next expectation to ever be triggered. MAT.
Expecting a call with parameter + defining other calls with any
Expecting a call with parameter + defining other calls with any
Mock a method that accepts a unique_ptr
That looks good. Thanks
Mock a method that accepts a unique_ptr
OK, implemented it in https://github.com/mat007/turtle/pull/35 Would you like to take a look before I merge the PR?
Sorry for not following up on this sooner. Yes you're right, it should be feasible, I'll fix it as soon as I can find a couple of hours.
I agree that that #2 would not work as some might expect, but I don't think devs familar with unique_ptrs would expect it to. I would expect the calls to get an empty unqiue_ptr. Once I have captured the unique_ptr I would accept responsibility for knowing that the value had been moved out. That's one reason I was thinking maybe a new constraint (capture, move? similar to returns vs moves) might be better, because then it would be more explicit that that value has been moved out. I agree that I can...
MOCK_CONSTRUCTOR calls default constructor
Thank you. I think this will fix my problem. I will try it this week. You can close this issue.
Hi jabaa, MOCK_BASE_CLASS actually declares a class, and if you don't intend to set expectations on the constructor you can simply do MOCK_BASE_CLASS(MConnection, Connection) { MConnection(std::string s1, std::string s2, std::string s3, std::string s4) : Connection(s1, s2, s3, s4) {} If you actually need to mock the constructor, add a manual call MOCK_BASE_CLASS(MConnection, Connection) { MConnection(std::string s1, std::string s2, std::string s3, std::string s4) : Connection(s1, s2, s3, s4) { Constructor(s1,...
I don't think there is a way we can make mock::retrieve safe in this case because of http://turtle.sourceforge.net/turtle/getting_started.html#turtle.getting_started.expectation_selection_algorithm Imagine the following code std::unique_ptr< … > p; MOCK_EXPECT( m ).with( mock::retrieve( p ), 3 ); MOCK_EXPECT( m ).with( mock::retrieve( p ), 7 ).calls( []( std::unique_ptr< … > pa ) { … } ); m( std::make_unique< … >( … ), 7 ); Here is what happens when m is being called : evaluate first expectation...
MOCK_CONSTRUCTOR calls default constructor
I took a quick look at the change. I see tests for calls and equal, does this also add support for retrieve? I can implement retrieve on top of calls myself, but the retrieve functionality is what I really want.
I believe https://github.com/mat007/turtle/pull/35 should fix the issue. The part you would more likelly be interested in would be https://github.com/mat007/turtle/pull/35/files#diff-585dc13369fc2ce33f49443a59c4e315R708 Does that look good to you? If it does I'll merge the pull request and you will be able to give it a go.
Indeed a unique_ptr still needs to behave correctly in non immutable constraints, including mock::equal. I'm still working on the details but I'm hoping to come up with a pull request in the next couple of days.
Would it be possible to create a constraint for unique_ptrs that compared against the pointer value stored (ake the value of the .get() method) instead of the actual unqiue_ptr, so you wouldn't have to move the unique_ptr to test the contraint?
I have made some progress on this issue and now it falls down to the following design decision. When a call occurs with actual parameters being passed to a mock object, among other checks (sequence, …) it first applies the constraints (e.g. what was passed to 'with') then if they "match" it performs the action (e.g. call a functor with the same signature as the mocked method). In case of a moved parameter this would mean forwarding to a constraint and then again to the action functor. Obviously this...
Adding support for retrieving the unique_ptr using mock::retrieve looks simple enough, see for instance https://github.com/mat007/turtle/pull/34 However I would have expected more plumbing to be needed to move the unique_ptr through the internals of turtle, e.g. from where you call container->accept( std::move( v ) ) up to the point where the constraint gets tested. For instance this fails to compile BOOST_AUTO_TEST_CASE( std_unique_ptr_is_supported_in_retrieve_constraint ) { MOCK_FUNCTOR( f, void(...
Hi Darin, Believe or not it seems this is the first time such a use case is being brought forward for turtle ! I'm afraid this is currently not supported in turtle… I will be having a look on how to add it. Thanks for the improvement suggestion!
I am a bit surprised by that, I thought using smart pointers was pretty common these days. I think I can make do with the managed_compare constraint for now. The container is a mock so I can return a different object then the one that was inserted and it shouldn't matter to the class being tested, but that does require a bit more work. Thank you for writing turtle. It is great. It has made our unit testing way easier and cleaner.
Hi Darin, Believe or not it seems this is the first time such a use case is being brought forward for turtle ! I'm afraid this currently not supported in turtle… I will be having a look on how to add it. Thanks for the improvement suggestion!
mock expectation on protected method
Closing it, re-open if you need further help on this.
Mock a method that accepts a unique_ptr
Mock a method that accepts a unique_ptr
mock expectation on protected method
Hi Gianni, You have to use the 'tag' e.g, MOCK_EXPECT(smoothingFrame()->mock_prot).once().returns(false); The reason you are not used to this is because when using MOCK_EXPECT the tag defaults to the method name, for instance MOCK_METHOD_EXT(validate, 0, bool(), validate); MAT.
mock expectation on protected method
Force a mock method to throw an exception
Thanks again Mathieu, I wasn't aware of the state sharing for copied objects. I verified it works. This case can be closed then. Regards, Gianni
Indeed this explains the issue. I believe you could also do PathMock* path = new PathMock(); MOCK_EXPECT(path->smooth).throws(Exception()); MOCK_EXPECT(path->clone).returns(new PathMock(*path)); because as stated in http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.creation mock objects can be copied and all copies then share the same state. MAT.
Indeed this explains the issue. I believe you could also do PathMock* path = new PathMock(); MOCK_EXPECT(path->smooth).throws(Exception()); MOCK_EXPECT(path->clone).returns(new PathMock(*path)); because as stated in http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.creation mock can be copied and all copies then share the same state. MAT.
Hi Mathieu, thanks for suggesting to make a self contained test: I realized the point! You can give a look at the attached file but basically I had: PathMock* path = new PathMock(); MOCK_EXPECT(path->smooth).throws(Exception()); MOCK_EXPECT(path->clone).returns(new PathMock()); instead I should have writted something like: PathMock path = new PathMock(); PathMock clonedPath = new PathMock(); MOCK_EXPECT(path->clone).returns(clonedPath); MOCK_EXPECT(clonedPath->smooth).throws(Exception()); In fact...
Hi Mathieu, I can make a try. Regards, Gianni
Hi, Do you think you might be able to make a small self contained test case I could investigate? Thanks, MAT.
Hi Mathieu, ExecutionException inherits from Exception which is a base class (it is a root class - no inhheritance). I noticed you have "once()" in addition wrt my example but I also tried with "once()" in vain. Cheers, Gianni
Hi Gianni, At first glace it looks like it should work, here is a test I just wrote to confirm it: namespace { MOCK_CLASS( my_mock ) { MOCK_METHOD_EXT( my_method, 1, int( int ), my_tag ) }; } BOOST_AUTO_TEST_CASE( basic_mock_object_usage ) { my_mock m; MOCK_EXPECT( m.my_tag ).once().throws( std::runtime_error( "some exception" ) ); BOOST_CHECK_THROW( m.my_method( 27 ), std::exception ); } How are Exception and ExecutionException defined ? MAT.
Force a mock method to throw an exception
mock::retrieve fails with boost::function
mock method returning rvalue reference
Thanks, it works! Gianni
mock method returning rvalue reference
Hi Gianni, You might want to use 'moves' instead of 'returns' e.g. MOCK_EXPECT(motionCompensator->eye).moves(osg::Vec3d());...
mock method returning rvalue reference
Build library
returns action customization
returns action customization
Hi Gianni, In order to do that you just need to use 'calls' instead of 'returns'....
returns action customization
Turtle 1.3.0 has been released !
Turtle 1.3.0 has been released !
Hi Loïc, Turtle is only available as a header-ony library, it cannot be built into...
Hello Mathieu, I'm fully agree: Boost can be used as header only, as turtle and Boost.Mock....
Build library
Hi, Sorry, for some reason I didn't get an email from SF notifying me of a new ticket...
Build library
Turtle 1.2.8 has been released !