<div dir="ltr"><div><div>It depends on how much work you are doing inside setBar method.<br><br>If you are simply setting self.bar to the input object, I don&#39;t see any reason to do any mocking. Mocking is useful is you have more than a simple assignment inside the method. If the logic is simple enough, doing the first one is enough.<br>
<br></div><div>Is that the template for the real code? Also, If you have to paste code into your mesage, make sure it is formmated such that you are not using tabs (8-space). Use 4-space. <br><br><br></div></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Mon, Jul 8, 2013 at 8:53 AM, Charles Medcoff <span dir="ltr">&lt;<a href="mailto:cmedcoff@hotmail.com" target="_blank">cmedcoff@hotmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The first test is what I would call a State based test without using any<br>
mocking library.  The second using arrange, act, assert using mock.  Works<br>
but seems clunky.  Is there a cleaner, more intuitive way?  setBar<br>
represents what might be some method that has logic to it and determine what<br>
(sub) type to do in an assignment.<br>
<br>
<br>
<br>
import unittest<br>
<br>
from mock import Mock, PropertyMock<br>
<br>
class Foo(object):<br>
<br>
               def setBar(self, bar):<br>
<br>
                              self.bar = bar<br>
<br>
class Bar(object):<br>
<br>
               pass<br>
<br>
<br>
<br>
class MyTestCase(unittest.TestCase):<br>
<br>
               def<br>
test_setBarOnFooResultsInCorrectType_StateBasedTesting(self):<br>
<br>
                              foo = Foo()<br>
<br>
                              foo.setBar(Bar())<br>
<br>
                              self.assertEqual(type(foo.bar), type(Bar()))<br>
<br>
               def test_setBarOnFooResultsInCorrectType_UsingMock(self):<br>
<br>
                              foo = Foo()<br>
<br>
                              mockBar = PropertyMock()<br>
<br>
                              type(foo).bar = mockBar<br>
<br>
                              foo.setBar(Bar())<br>
<br>
                              mockBar.assert_called_once()<br>
<br>
                              arg, junk = mockBar.call_args<br>
<br>
                              self.assertEqual(type(arg[0]), type(Bar()))<br>
<br>
<br>
<br>
if __name__ == &#39;__main__&#39;:<br>
<br>
               unittest.main()<br>
<br>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br></div>