keeganwitt / gmock

Automatically exported from code.google.com/p/gmock
6 stars 2 forks source link

Support mocking constructor to throw an exception #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We should think out a good DSL to support throwing an exception while 
invoking the constructor.

Original issue reported on code.google.com by JohnnyJianHY on 14 Oct 2008 at 6:45

GoogleCodeExporter commented 9 years ago
Suggestion:
    def mock1 = mock(Loader, constructor: IllegalArgumentException)
    def mock2 = mock(Loader, constructor: new IllegalArgumentException
("message")) // force to new an exception if parameters are needed

Original comment by JohnnyJianHY on 14 Oct 2008 at 6:51

GoogleCodeExporter commented 9 years ago
But how to match parameters of constructor?

Original comment by JohnnyJianHY on 14 Oct 2008 at 6:52

GoogleCodeExporter commented 9 years ago
Indeed. The expection should be raise only on matching constructor. What about:

def mockLoader = mock(Loader, constructor:[1,2]).raises(new Exception())

The raises keyword should then only be used when using constructor:.

Original comment by julien.g...@gmail.com on 14 Oct 2008 at 6:58

GoogleCodeExporter commented 9 years ago
It is good and consistent with the current API. But I just worry that is it 
readable 
and easy to understand for most users?

Original comment by JohnnyJianHY on 14 Oct 2008 at 7:06

GoogleCodeExporter commented 9 years ago
Wait, it will have to add a "raises" method to Mock, then it stops users to 
mock a 
method named "raises".
Consider that
    def mockLoader = mock(Loader, constructor:[1,2]).raises(new Exception())
is equal to
    def mockLoader = mock(Loader, constructor:[1,2])
    mockLoader.raises(new Exception())

Original comment by JohnnyJianHY on 14 Oct 2008 at 7:11

GoogleCodeExporter commented 9 years ago
You are absolutely right. Well spotted.

What about:
mock(Loader, constructor:[1,2], raises(new Exception())

So we are still consistent with the API.

Original comment by julien.g...@gmail.com on 14 Oct 2008 at 7:29

GoogleCodeExporter commented 9 years ago
Should we reconsider the DSL of constructor? Because it seems to become 
unreadable 
if we keep on like that.

Suggestion:
    def mock1 = mock(Loader) { constructor(1, 2) }
    def mock2 = mock(Loader) {
        constructor(1, 2).raises(Exception)
    }

What's more, consider strict order mocking, if someone expects that a static 
method 
should be invoked before the constructor do, then our solution will not work.

Original comment by JohnnyJianHY on 14 Oct 2008 at 7:40

GoogleCodeExporter commented 9 years ago
As properties named "new" is supported since Groovy 1.6, we can build a DSL 
like:
    def mock = mock(Loader)
    mock.new(1, 2)
    mock.new(1, 2).raises(Exception)

Then we can add a method named "getNew" to Mock, which returns a closure.

Original comment by JohnnyJianHY on 14 Oct 2008 at 8:46

GoogleCodeExporter commented 9 years ago
I think I quite like your later proposal. It's not as intuitive to me as the 
current
behavior but it will greatly help for the static/strict mocking.

So let's go for mock.new(1,2) and see if it work,

Original comment by julien.g...@gmail.com on 15 Oct 2008 at 7:36

GoogleCodeExporter commented 9 years ago
OK, but we have to update to Groovy 1.6 beta 2.

Original comment by JohnnyJianHY on 15 Oct 2008 at 8:26

GoogleCodeExporter commented 9 years ago
I missed the Groovy 1.6 point sorry. We can't really use it grails is still on 
1.5. We'll have with something 
else. 

Original comment by julien.g...@gmail.com on 15 Oct 2008 at 5:20

GoogleCodeExporter commented 9 years ago
In Groovy 1.5, we can still mock like:
    def mock = mock(Loader)
    mock.'new'(1, 2)

Original comment by JohnnyJianHY on 16 Oct 2008 at 10:59

GoogleCodeExporter commented 9 years ago
Any thought?

Original comment by JohnnyJianHY on 17 Oct 2008 at 9:40

GoogleCodeExporter commented 9 years ago
I am not entirely convinced here. mock.'new'(1, 2) is not the most intuitive 
part of
our dsl. Should we move this to a latter release to give us more time and some
external thought.

Original comment by julien.g...@gmail.com on 17 Oct 2008 at 3:53

GoogleCodeExporter commented 9 years ago
OK, two releases later. Hope Groovy 1.6 and Grails 1.1 will be released before 
that.

Original comment by JohnnyJianHY on 17 Oct 2008 at 5:23

GoogleCodeExporter commented 9 years ago

Original comment by julien.g...@gmail.com on 25 Dec 2008 at 3:25

GoogleCodeExporter commented 9 years ago

Original comment by JohnnyJianHY on 26 Dec 2008 at 5:02

GoogleCodeExporter commented 9 years ago

Original comment by julien.g...@gmail.com on 26 Dec 2008 at 10:58