2

I'm new to Node.js and in general new to testing frameworks / methods in Javascript. So far I'm thinking of giving vows a try.

Specifically, I'd like to be able to stub / mock my data sources. I'm considering two situations:

  1. Stub out the whole module
  2. Stub out just a single function when for whatever reason stubbing the whole module doesn't work.

Any suggestions or examples for the approach you found cleanest and simplest to use?

Jonas
  • 121,568
  • 97
  • 310
  • 388
gregsilin
  • 287
  • 1
  • 7
  • 1
    Depends on the architecture. You should have a datasource module which you can stub out. There are libraries that make emulating certain data sources easier (I know theres a mocking library for couchdb). Personally I dont mock out data source, I just write integration tests – Raynos Dec 08 '11 at 01:39
  • Raynos, thanks for the comment. In our case, we definitely want to stub out at least some tests. I am asking for advice specifically on stubbing out node.js modules and/or functions. Latter has quite a bit of examples on the web, I'd love some thoughts on best practices.. – gregsilin Dec 08 '11 at 01:44

1 Answers1

0

Personally, I have been enjoying using Mocha + Chai + Sinon as my stack. It is a headache to set up at first (sinon especially..), but once you get it up, everything feels so natural.

Sinon is a very powerful library that allows you to fake http requests or even the server. You get a choice between stubs/spies/and mocks. About the only thing I have found that it doesn't do is dom-based event mocking..

If I were you, I would load up the modules as they are and stub/spy on their behavior with Sinon. I know some people have attempted to load up entire mock modules, but I personally think that is too intrusive to the code you're testing.

badunk
  • 4,310
  • 5
  • 27
  • 47