Jonathan Vanasco wrote:
> I have several helper classes that use g from pylons
>   
You have a design problem then :) pylons.g is really a substitute for
module-level singletons so you can have them associated to an app's
instance and have several of these coexisting in the same process
without conflicts (eg: several blog instances each with it's own db
connection to a different database). This is actually the same problem
you would have if you depended on global state: More difficult to
isolate tests, more difficult to set them up, etc...

If you really want to go this route you can either mock it (using the
same pattern as has been suggested to mock pylons.session) or test the
whole stack so PylonsApp takes care of setting up and tearing down the
app context on every request.

app = paste.deploy.loadapp('config:mytestconfig.ini')
app = webtest.TestApp(app)

resp = app.get('/some/test/method')
assert resp.some_test_var == 'foo'

the controller action that responds to the above url can place variables
in environ['paste.testing_variables'] and they'd be bound to the test
response that TestApp.{get,post,put,etc...} returns:

def some_controller_method(self):*
    if* *'paste.testing_variables'* *in* request.environ:
         request.environ[*'paste.testing_variables'*][*'some_test_var'*]
= 'foo'

However, the "right" way to do it, IMHO, would be to separate the
functions/classes that depend on g in two. For example, say you needed a
pylons.g.db_connection inside a helper function "bar":

def side_effect_free_bar(db_connection):
    # do something with db_connection and refrain from accessing global
state
    # this should be pretty easy to unit-test since it's output depends
solely on it's arguments.
    # You can easily pass a db_connection you've explicitly prepared for
testing

def bar():
    # This one you would test inside functional/integration tests, if at
all.
    return side_effect_free_bar(pylons.g.db_connection)

HTH,
Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to