开发者

Friction in Settingup test using abstract classes rather than Interfaces

开发者 https://www.devze.com 2023-01-12 07:01 出处:网络
I have started following Test Driven Development and find it very logical and helps me a lot. I have one question

I have started following Test Driven Development and find it very logical and helps me a lot.

I have one question

1) In lot of StackOverflow answers I have seen that people say abstract base class adds lot of friction to setup a test. If we use interfaces, there would be less friction. What I am thinking is, if there is no default implementation in Abstract classes how does this make any difference in setting up the test.

For Eg : HttpContext ... it exposes lot of properties which needs to be setup (read queryString,FormCollection etc). Here you have to mock all these things before you can actually use the HttpContext in a unit tes开发者_Python百科t. (I prefer using moq)

If someone can help me understand why this adds more friction, it would be helpful.

--RN


I can only think of one relevant difference with respect to unit testing (if we're ruling out default implementations): an abstract classes' methods may be sealed,* in which case they cannot be mocked, while interfaces never have this problem.

* Or final, or not declared virtual in the first place depending on the language.

0

精彩评论

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