Permalink
Browse files

Ripped out StubClass and fixed socket examples

  • Loading branch information...
1 parent 336aeab commit 88e1329eea4cf4ac0f62d11d941cca5c9cb98a97 @vbabiy vbabiy committed Mar 1, 2011
Showing with 16 additions and 69 deletions.
  1. +3 −0 chai/chai.py
  2. +1 −0 chai/mock.py
  3. +1 −33 chai/stub.py
  4. +11 −3 examples/examples.py
  5. +0 −33 tests/stub_test.py
View
@@ -116,6 +116,9 @@ def mock(self, obj=None, attr=None):
'''
rval = Mock()
if obj!=None and attr!=None:
+ rval._object = obj
+ rval._attr = attr
+
if hasattr(obj,attr):
orig = getattr(obj, attr)
self._mocks.append( (obj,attr,orig) )
View
@@ -4,6 +4,7 @@
from types import MethodType
from stub import stub, Stub
+
class Mock(object):
'''
A class where all calls are stubbed.
View
@@ -34,9 +34,6 @@ def _stub_attr(obj, attr_name):
if isinstance(attr, property):
return StubProperty(obj, attr_name)
- if isinstance(attr, type):
- return StubClass(obj, attr_name)
-
if isinstance(attr, types.MethodType):
# Handle differently if unbound because it's an implicit "any instance"
if attr.im_self==None:
@@ -68,9 +65,6 @@ def _stub_obj(obj):
if isinstance(obj, property):
raise UnsupportedStub("must call stub(obj,attr) for properties")
- if isinstance(obj, type):
- return StubClass(obj)
-
# I thought that types.UnboundMethodType differentiated these cases but
# apparently not.
if isinstance(obj, types.MethodType):
@@ -87,7 +81,7 @@ def _stub_obj(obj):
if type(obj).__name__ == 'wrapper_descriptor':
raise UnsupportedStub("must call stub(obj,'%s') for slot wrapper on %s",
obj.__name__, obj.__objclass__.__name__ )
-
+
raise UnsupportedStub("can't stub %s", obj)
class Stub(object):
@@ -233,29 +227,3 @@ def teardown(self):
'''
setattr( self._obj, self._attr, self._orig )
-class StubClass(Stub):
- '''
- Stub a class.
- '''
-
- def __init__(self, module_or_class, name=None):
- '''
- Initialize with an object that is a method wrapper.
- '''
- super(StubClass,self).__init__(module_or_class, name)
- if inspect.isclass(module_or_class):
- # Working with class
- self._class = module_or_class
- self._attr = self._class.__name__
- self._obj = inspect.getmodule(module_or_class)
- else:
- # Working with module
- self._class = getattr(module_or_class, name)
-
- setattr( self._obj, self._attr, self )
-
- def teardown(self):
- '''
- Replace the original method.
- '''
- setattr( self._obj, self._attr, self._class )
View
@@ -10,19 +10,27 @@ def connect():
sock.bind(('127.0.0.1', 10000))
return sock.recv(1024)
+def get_host():
+ return socket.gethostname()
+
class SocketTestCase(Chai):
def test_socket(self):
mock_socket = self.mock()
- self.expect(socket, 'socket').returns(mock_socket)
- # Note: Unfortunately we can't use self.expect(socket.socket) till python 3
- # This is due to the way that the socket module is implemented in python 2
+ self.mock(socket, 'socket')
+ self.expect(socket.socket.__call__).returns(mock_socket)
self.expect(mock_socket.bind).args(('127.0.0.1' , 10000))
self.expect(mock_socket.recv).args(1024).returns("HELLO WORLD")
self.assert_equals(connect(), "HELLO WORLD")
+ def test_get_host(self):
+ self.mock(socket, 'gethostname')
+ self.expect(socket.gethostname.__call__).returns("my_host")
+ self.assert_equals(get_host(), "my_host")
+
+
######################################
## Mocking datetime.now
######################################
View
@@ -238,36 +238,3 @@ def __call__(self, *args, **kwargs):
f2.bar()
self.assertEquals( 2, s.calls )
-
-
-class StubClassTest(unittest.TestCase):
- # NOTE: Make sure you always teardown your stub when adding tests to this case.
-
- def test_init_with_module_and_name(self):
- org = samples.SampleBase
- s = StubClass(samples, 'SampleBase')
- self.assertEquals(s._class, org)
- self.assertEquals(s._obj, samples)
- self.assertEquals(s._attr, "SampleBase")
- s.teardown()
-
- def test_init_with_class(self):
- org = samples.SampleBase
- s = StubClass(samples.SampleBase)
- self.assertEquals(s, samples.SampleBase)
- self.assertEquals(s._class, org)
- self.assertEquals(s._obj, samples)
- self.assertEquals(s._attr, "SampleBase")
- s.teardown()
-
- def test_teardown_with_module(self):
- org = samples.SampleBase
- s = StubClass(samples, 'SampleBase')
- s.teardown()
- self.assertEquals(samples.SampleBase, org)
-
- def test_teardown_with_class(self):
- org = samples.SampleBase
- s = StubClass(samples.SampleBase)
- s.teardown()
- self.assertEquals(samples.SampleBase, org)

0 comments on commit 88e1329

Please sign in to comment.