开发者

The case for mocking business objects

I am trying to see the benefit of mocking the business layer. When testing, you are verifying 开发者_StackOverflow社区your business logic so you need to mock the data layer.

So what would be the benefit of mocking a business layer?


I'm assuming by "mock" you mean any kind of stand-in. When I hear mock I think expectations (e.g. mock.should_recieve :do_something), but your mention of "mocking" data sounds like a stub (i.e. returning canned values).

There is a spectrum of philosophy from "fake everything" to "fake only if you really, really need to". One rationale sited for using doubles all the time is to speed up tests (which may be a bad argument - see this amazing presentation) and to isolate failures.

In the case you mentioned - coding the data layer - some benefits in addition to those above could be:

  • you haven't written the business layer yet, so you use doubles to enable you to write the data layer (which you may replace with real objects depending on which camp you're in
  • you use mocks to design conversations among collaborators (Liz Keogh has some excellent stuff explaining this)

HTH, Sean


If you are unit testing some code in the presentation layer you might want to mock the business layer. You mock the parts of your code that you aren't testing in that test.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜