derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

dictionary item 'patching' #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A way of temporarily adding or changing items in a dictionary purely within the 
scope of the test. e.g. for modifying os.environ.

Would take a copy of the dict before patching and then clear and restore the 
dictionary on exit.

{{{

@patch.dict(os.environ)
def test_something():
    os.environ['foo'] = 'bar'

@patch.dict(os.environ, ('key', 'value'), ('key2', 'value2'))
def test_something():
    assert os.environ['key'] == 'value'
}}}

Original issue reported on code.google.com by fuzzyman on 12 Jun 2010 at 11:45

GoogleCodeExporter commented 9 years ago

Original comment by fuzzyman on 13 Jun 2010 at 8:44

GoogleCodeExporter commented 9 years ago
Why:
  @patch.dict(os.environ, ('key', 'value'), ('key2', 'value2'))
and not:
  @patch.dict(os.environ, {"key": "value", "key2": "value2"})

?

Original comment by kon...@gmail.com on 15 Jun 2010 at 12:20

GoogleCodeExporter commented 9 years ago
Hmmm... good point. I just thought of overriding values in terms of key value 
pairs. The dict API is probably more logical.

Original comment by fuzzyman on 15 Jun 2010 at 12:35

GoogleCodeExporter commented 9 years ago

Original comment by fuzzyman on 24 Jun 2010 at 3:15