NSubstitute vs Moq - a quick comparison

Posted on Mon 27 February 2012 in Coding • Tagged with mock, moq, nsubstitute, test

When you write unit tests, you occasionally need to mock away dependencies of the subject under test (SUT). By far the easiest way to do this is to use a mocking library, which has the added benefit that it allows you to verify the behavior of the SUT by inspecting ...


Continue reading

Mocking iteration over file in Python

Posted on Sat 24 December 2011 in Coding • Tagged with file, mock, test, Python

Let’s say that I want to mock reading from a file using file’s iterator capability, i.e. (simplified version):

def readfile(f):
    using open(f) as fd:
        for line in fd:
            yield line

I could mock the readfile function, but then my mock would have to replicate the ...


Continue reading

Mocking Zope interfaces

Posted on Wed 28 September 2011 in Coding • Tagged with mock, test, python

In one of my pet projects I use Twisted for handling asynchronous network events. While browsing the Twisted source code, I encountered Zope interfaces. In a dynamically typed language like Python, what good are interfaces?

It turns out that Zope interfaces are slightly different from interfaces found in a statically ...


Continue reading