Denis Kudriashov wrote:
Hi Herby.
2017-10-20 18:49 GMT+02:00 Herby VojÄÃk <herby@mailbox.sk <mailto:herby@mailbox.sk>>:
I had this problem. I tried something like (though not exactly w/ this code):
Foo stub new will: [ :aMessage | | original | original := MockExpectedOriginalCall new executeFor: aMessage. original stub. ^ original ]
but IIRC it failed on doing #stub inside will: block.
So the arguments of block in message #will: are expected to be arguments of stubbing message (not a full message instance). We can introduce new kind of expected action to automatically stub any result of message send:
Foo stub new willStubRealResult.
Yeah, exactly, something like that is useful.
With help of new subclass of MockExpectedOriginalMethodCall:
MockExpectedMethodResultStub>>executeFor: anOccurredMessage
realMethodResult := super executeFor: anOccurredMessage.
realMethodResult stub.
^realMethodResult.
MockExpectedMessage>>willStubRealResult
self will: MockExpectedMethodResultStub new
Actually I tried this path myself but it failed badly during doing the sub-#stub with lots of "go one metalevel down" etc. calls in stack, so I gave up with "I don't understand this to make it work atm". Does your solution actually work?
And then you will be able specify expectations for all Foo instances:
(Instance of: Foo) stub someMessage willReturn: #const
As in, would `Foo new someMessage should be: #const`? In that case, great and thanks.
And you will be able assert any message which was sent to Foo instances:
(Instance of: Foo) should receive someRequiredMessage which should equal: #expectedValue
I committed this code to the dev branch. But I am wondering that such kind of behaviour is really needed. Probably it is useful as you ask about it.
Herby