开发者

Unit testing - testing in isolation

开发者 https://www.devze.com 2022-12-19 11:20 出处:网络
I\'ve got a set up with a number of layers: Website Application / Service Domain (contains entities) Persistence (contains repositories)

I've got a set up with a number of layers:

  • Website
  • Application / Service
  • Domain (contains entities)
  • Persistence (contains repositories)

I'm testing the persistence layer in isolation OK using data created in memory from a stub object.

Now, Im thinking about testing my Website layer. I know I should be testing it in isolation which Im thinking means creating a stub for the Application layer object it uses but this stub would need its own set of in memory data, duplicated in the stub in the persistence layer and I dont want to do duplicat开发者_如何学Goe this and manage it.

So my question is should the subject under test always work with stub objects from the layer below in order to be isolated and do they normally have their own set of data? Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data?

Thanks for your help. This feels like the last bit of the puzzle for me ...


Ideally in unit testing each subject under test is isolated from its dependencies. You do not want to think your subject under test is broken because one of its dependencies broke and caused the subject under test to fail. If you test like this, you might spend a lot of time tracking down bugs in the wrong place.

Testing how things operate together is the domain of integration testing, not unit testing


Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data?

If you do this, I wouldn't call the test a unit test in isolation anymore - if the test fails, where is the bug? - but an integration test. Don't misinterpret me, integration testing is fine too, it just has another purpose. But if your goal is to unit test the website layer in isolation, you should mock/stub direct dependencies.


Setting up test data can be tedious task. If you are using DotNet you can make use of a library called NBuilder to generate test data very easily and quickly. It supports a nice fluent interface. You can read more about it here.

0

精彩评论

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