Hacker News new | past | comments | ask | show | jobs | submit login

My take is that your business logic shouldn't know about your storage tech. (Like, dependency inversion 101 right?)

> Still, using an in-memory db is way better than mocking, the tests are not coupled to the implementation details/the underlying model.

Isn't this backwards? The fact that you've backed your storage with a HashMap (which is 100% what I shoot for too) means your service-under-test cannot know if it's talking to an SQL database.




I think we're talking about the same thing :D? The service/business logic/whatever you might want to call it interacts with the storage via an interface - in prodcution that interface is implemented by a component that can talk to a real SQL database, in tests I can just create a wrapper around a hash map and use that.

EDIT: What I meant when I wrote that tests are not coupled to the underlying model/details is that with a mock you have to explicitly specify "when called with this return that". With an in memory database implementation you don't need to do anything, the code will just use the interface methods like "getX" or "saveY".


> with a mock you have to explicitly specify "when called with this return that".

Riiiight, no I've always hated that Mockito way of doing things.

I find wrapping a hashmap avoids the need for explicit when-this-then-that bindings, because a hashmap already does the expected behaviour natively.

You can even 'integrate' your tests as deeply as you like, all standing on top of your poor hashmap. I.e. Http tests with unit test speed.

var controller = new Controller(new Service(new Repo(new HashMap()))) controller.POST(...); assert(controller.GET(...));




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

Search: