Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm new to testing but wholeheartedly realize how important it is. The main issue is that my company has no top-down support for testing at all. That is we don't have any unit testing and just a bit of human testing (I hope I'm using the right terminologies).

I have been trying to get testing started here and have to do it subtly because I'm often asked "when will feature X be done" and therefore can't start the week(s) long project of writing tests for all of our legacy code. Which would need to get rewritten just to provide seams for testing (if I have the concept of 'seams' right). I can, however, sneak a bit of time to test one function. Or at least I think I can.

I'm writing code that basically processes quite a bit of data and then renders it using D3. I would like to write a test for one of the most important functions, binData. It takes one optional argument (the date to start binning from) and accesses a few properties like this.rows and this.timestamps.

How would I go about doing that?

I think I'll need to create a mock object to hold this.data and this.timestamps (and the other accessed properties). In fact, I imagine several different mock objects which would allow testing of several different datasets. But other than that I'm kinda stuck. In terms of frameworks I've heard of Mocha and Jasmine and Vows but don't know which would be best.

I also don't know how to tie into the massive (and complex) class and module hierarchy and to meet each object's dependencies. That is, the function binData is in a file called, say, dataBinner.js which requires all sort of other functions in other files but doesn't explicitly include anything on it's own. Instead, it itself is included in a "manager" file which includes everything that dataBinner.js and the other modules will need. I suppose I'll need to find a way to instantiate everything necessary to test binData.

I'm not afraid to do a bit (or a lot) of reading to better understand things. Any help regarding this specific task of writing a test for binData or the larger question of how to slowly implement testing in a non-test-friendly place would be much appreciated.

CLARIFICATION

I'm 100% on board, testing-wise. I know it's important and I know that my company is incorrect by not having testing. You'd expect emails that say "fixed regression caused by fixing bug 1413" would trigger something. Or that things that shouldn't take that long take waaaaaay too long ('cause it's spaghetti legacy code).

What I'm asking is given that how do I test my one function which relies on the legacy spaghetti code. That is, how does someone inject a bit of testing into a place without it? I'm guessing that I'll have to write my own testing framework because I'll have to create mocks for the spaghetti legacy code. So that I can instantiate my object and call my function outside of the scope of the application.

share|improve this question
    
Are you saying you don't have a test environment? With a test database, etc.? –  BobDalgleish Mar 12 at 15:15
    
That's right. As the answers put it, we have no culture of testing at all here. We have regressions all the time. It's .. inefficient ;) I'm asking what, given all that, to do. –  ari gold Mar 12 at 17:36

2 Answers 2

Your problem is larger than mocking a single function.

It doesn't seem like your company has a "culture of testing." This will make it difficult to make changes "in the large," because your company expects things to happen in a certain time frame, because that's how long they took without testing.

Your best bet might be to:

  1. Refactor old methods and add tests to them during modification or addition of new features, and/or

  2. Wall-off the old code-base as much as possible, adding new features only as part of a Test-Driven methodology in a green field. Talk to the old code, but don't change it.

share|improve this answer

I'm afraid I have to agree with @Robert Harvey in that the lack of a culture of testing is your real problem. From that point of view, it's not really about mocking or getting your test to work, but on deciding whether or not you want to change this culture in your company. Be aware, that cultural changes are hard - as in almost frustratingly hard.

I suggest you do a lot of reading if you want to tackle this challenge. You must not be dumb-founded when met with resistance, but should have enough knowledge on your back to counter any arguments your managers may come up with.

Here are some particularly interesting reads in that regard:

  • Michael Feathers - Working with Legacy code: This one is interesting for the mere fact of his definition of legacy code. He states (and I paraphrase here) that

Legacy code is code without tests.

Based on that definition, your company is currently in the business of actively developing legacy code. Way to go.

  • Robert C. Martin's books about Clean Code. The first book is more about technical details, interesting for developers. However, his second book deals with professionalism in our workspace, and he makes a few important points about what it means to be a professional developer and how that ties into writing tests and holding deadlines. This book in particular will give you a lot of things to wrap your head around - and a hell of a lot of attack vectors on your current company culture's professional behavior.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.