c# - Is it ok to unit-test an abstract class by instantiating it as a mock object? -


it has come attention can unit-test abstract class instantiating mock object. thereby mock abstract properties , methods, while being able test implemented ones.

however, accustomed distinguish between class to test , injected dependencies to mock/stub. so, in stage of new enlightenment wonder if there pitfalls way of testing?? thoughts on this?

regards, morten

an abstract class, of value, should have concrete subclasses can instantiated. unit test those, , via them implicitly base class.

if has no concrete subclasses, can't think of reason why should exist (as abstract class, is).

in general, prefer use mocking set environment of class tested, not instantiate class itself. distinction - me - keeps test cases clearer, , ensures test real functionality of class.

of course, can think of cases (with legacy code) when main issue able write unit tests somehow, anyhow, enable refactoring (as discussed in working legacy code). temporary solution in such cases, (almost) goes. once unit tests working, class should refactored asap make clean , testable (and unit tests too, make them maintainable).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -