开发者

What is the correct way to do this using TDD principles?

开发者 https://www.devze.com 2023-01-05 06:59 出处:网络
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\'

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开发者_JAVA百科 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!


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消