Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

We really need some better solutions to this. Aside from abstraction it also comes with the performance penalty of virtual functions everywhere.

I think we had some good tools to solve this but either threw them away in newer languages or just forgot they were possible. Working with c recently I wanted to mock something and the language obviously couldn't help me so I came up with something along the lines of:

  #ifdef TEST
    #define foo mock_foo
  #endif
This worked for mocking single functions but would need to evolve for more complex code. There are a number of ways to do this from optional includes to compiling test fixtures to their own binaries with the mock implementation being the actual implementation. Both options should work with just about any language to varying degrees of effort.

The biggest barrier to approaches like this is the reliance on IDE's and what they can do. I've never seen an IDE that can handle rules like this, they want to compile the project and control it's structure. In c# for instance, it would be pretty easy to have a mock library with concrete alternative implementations and compile the tests individually, something like:

  csc.exe MyTest.cs MyClassUnderTest.cs /reference MyMockAssembly.dll
This is perhaps even easier in c/c++ where the includes are more explicit. Once you take back control of the compilation process a lot more options open up though.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: