Issue 15 in pymox: CreateMock should support arbitrary attribute setting

8 views
Skip to first unread message

codesite...@google.com

unread,
Dec 11, 2009, 8:49:08 AM12/11/09
to mox-d...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 15 by alan.franzoni: CreateMock should support arbitrary
attribute setting
http://code.google.com/p/pymox/issues/detail?id=15

At the present time, Mox has one shortcoming: whenever you need to set
attributes on an instance, you need to create the mock object and then set
attributes on it.

This is often the case for attributes that are usually set in the object's
__init__ method and can't be easily inferred by CreateMock, e.g.

class MyClass(object):
def __init__(self):
self.a = 1

# you must mock it this way:

m = Mox()
mockobj = m.CreateMock(MyClass)
mockobj.a = 1

even though this is usually done in setUp(), it might get boring and make
unit tests less and less readable.

This patch lets arbitrary kw args to be supplied to createmock, e.g.

mockobj = m.CreateMock(MyClass, a=1)
self.assertEquals(1, mockobj.a)

This makes it easy to both add instance attributes and to change instance
attribute values from the default (class defined) value.


Attachments:
mox-arbitrary-attributes.patch 3.8 KB

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

codesite...@google.com

unread,
Dec 21, 2009, 12:01:20 PM12/21/09
to mox-d...@googlegroups.com

Comment #1 on issue 15 by steve.middlekauff: CreateMock should support

I'm not a huge fan of this syntax, but the proposal does make sense. I
wonder if it
might make more sense to provide a MockOptions object to the ctor, which
could be
setup with these params, and possibly other configuration? or maybe just a
named
argument that takes a dict?

I'm not sure, but I just don't want to paint us into a corner with an
overly complicated
ctor down the line.

d = { 'a': 1}
m.CreateMock(MyClass, attrs=d)

codesite...@google.com

unread,
Dec 22, 2009, 3:28:39 AM12/22/09
to mox-d...@googlegroups.com

Comment #2 on issue 15 by alan.franzoni: CreateMock should support

Well... I think it's a matter of test case design, I don't really know how
this is used at your facilities.

I rarely create the same mock object multiple times while in a test - if I
have the need to do so, I'll probably set
it up in setUp() . Of course you fear that this change might prevent any
other possibile argument to be passed to
CreateMock in the future, because the kwargs behaviour could break existing
unit tests.

Hence I think the "attrs" version makes perfect sense and it's all right,
just say so if you prefer me to code the
patch.

codesite...@google.com

unread,
Dec 22, 2009, 3:42:44 AM12/22/09
to mox-d...@googlegroups.com

Comment #3 on issue 15 by steve.middlekauff: CreateMock should support

If you want to put together the "attrs" version, I'd be happy to look it
over and accept it.

codesite...@google.com

unread,
Dec 22, 2009, 6:11:25 AM12/22/09
to mox-d...@googlegroups.com

Comment #4 on issue 15 by alan.franzoni: CreateMock should support

Here it is.


Attachments:
mox-attrs.patch 3.7 KB

codesite...@google.com

unread,
Jan 8, 2010, 6:31:21 PM1/8/10
to mox-d...@googlegroups.com

Comment #5 on issue 15 by steve.middlekauff: CreateMock should support

Sorry for the huge delay on this. I just patched this, and
testCantMockPrivateAttributes is failing. I'm looking into it now.

codesite...@google.com

unread,
Jan 8, 2010, 7:00:25 PM1/8/10
to mox-d...@googlegroups.com
Updates:
Status: Fixed

Comment #6 on issue 15 by steve.middlekauff: CreateMock should support

I fixed the issue and submitted your patch. Thanks again!

Reply all
Reply to author
Forward
0 new messages