views:

80

answers:

2

I'm new to TDD, and I'm trying to learn the right way to do things. So I have to make a javascript class for a web browser extension that stores the user's information to HTML5's localStorage (I'll call this class UserInfoStorage). However, I want to make my extension cross-browser compatible, and the way to interact with localStorage is different across the browsers (Chrome, Firefox, Safari).

Should I make another class called Storage that is specific for each browser and have UserInfoStorage use this to store stuff to localStorage? How would I unit-test this without tying the test for UserInfoStorage to the implementation of Storage?

Thanks!

+2  A: 

In general terms you would mock Storage under test to ensure that the UserInfoStorage interacts with it correctly. Those would comprise (part of) your unit tests on UserInfoStorage. Then you would have separate unit tests for each of the Storage implementations per browser, and ideally also tests on the code which determines which Storage implementation to provide according to the browser involved.

Yishai
A: 

I realized that the best way to do this would be to create a test Storage that has a basic get and set functionality (kind of like a test database), and have UserInfoStorage use this to store and retrieve data. This allows for testing the interface of UserInfoStorage directly, without tying it to the implementation of Storage, so if I change the way UserInfoStorage works, the tests will still be valid (since they are not testing whether UserInfoStorage interacts with Storage correctly, but rather that the functions of UserInfoStorage produce the desired results).

Chetan