Unit testing with Twisted: testing protocols

I had some hard time when testing Twisted protocols. Although they should be decoupled from factories, most examples I could find, including those from the official doc, were just too noisy and suggested using a factory.

I don't like that approach - I should not be forced to use a factory - that creates an unnecessary coupling - let's just pretend a factory existed and creates a connections, that's all our protocol should need.

Of course sometimes a protocol is bound to a very specific factory since it needs to invoke methods on it for non protocol-specific data (remember, there's just one factory, while there's one protocol per connection). I don't like that behaviour, since it creates an unnecessary coupling and makes factories huge and not cohesive, and I think protocols should just be injected whatever peers they need during the buildProtocol() invocation - but I'll talk about that in another post. In the meantime, I reccommend just creating a mock factory and setting it manually on the protocol.

So, here it is the solution I baked:

What do you think about this solution?

Alan Franzoni

Read more posts by this author.

Trieste, Italy
comments powered by Disqus