heynemann / pyvows

Python implementation of Vows.js

Home Page:http://pyvows.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: make `Vows.async_topic` more human-friendly

Zearin opened this issue · comments

I’ve been stuck on figuring out how to get @Vows.async_topic to work. I’ll keep trying, but in the meantime I had some ideas that would make it much easier for other people using PyVows.

  • Make @Vows.async_topic insert callback as a keyword argument. Example:
class ThingVows(Vows.Context):
    @Vows.async_topic
    def topic(self, parent_topic, callback=some_function):
        ...
  • Allow @Vows.async_topic to accept arguments. Example:
class ThingVows(Vows.Context):
    @Vows.async_topic(callback=some_function)
    def topic(self, parent_topic):
        ...

I think the second example would be best.

Sorry for the misunderstanding in the documentation.

You are not supposed to pass a callback to pyvows. That’s the callback you
are supposed to call your data with.

Imagine I have a method called async_get_resource that I want to test. The
signature for that method is:

def async_get_resource(url, on_complete_handler):
    asyncHttpLib.get(url, on_complete_handler)

This method would be very hard to test, because when it completes it
returns None. There's nothing to return at this point.

That's where pyvows async_topic comes in.

Testing it with pyvows would be trivial. Just pass the callback pyvows
provides as the callback for the function being tested and pyvows figures
the rest out. For the above example:

class AsyncGetResourceVows(Vows.Context):
    @Vows.async_topic
    def topic(self, callback):
        async_get_resource('http://google.com', callback)

    
def should_return_proper_html(self, topic);
# here the topic has all the arguments that the callback was called with. In our case, data
expect(topic[0]).to_include('google')

Is it any clearer?

Cheers,
Bernardo Heynemann

Ah, yes. I think I understand now—unless the code I’m testing already uses a callback, I shouldn’t need async_topic.

Is that correct?

Exactly! :)

Cheers,
Bernardo Heynemann