substitute for abstract class(methods)

767 views
Skip to first unread message

tasos foobar

unread,
Jun 3, 2015, 1:57:40 PM6/3/15
to nsubs...@googlegroups.com
Hello!
I know that nsub prefers interfaces(and virtual methods) , so my question is can i create a substitute for an abstract class and for example
return a specific value(while testing) from an abstract method of this class? Is this ok or dangerous?
Thanks!

David Tchepak

unread,
Jun 3, 2015, 7:49:00 PM6/3/15
to nsubs...@googlegroups.com
Hi,
Yes, you can create a substitute for an abstract class. You will be able to set specific values for any `abstract` or `virtual` members of the class. The potential problem is that real code can be executed at test time, including:
* constructor logic
* non-virtual members
This may or not be a problem for you, depending on the nature of the real code being executed, and how careful you are in calling virtual/non-virtual members during your tests. The other issue is that if you do call a `non-virtual` member without realising it (say, a member is changed from `virtual` to non-virtual), NSubstitute can get confused, producing unexpected behaviour or tricky-to-debug errors.

To be safest you can make sure the constructor code doesn't do anything too side-effecty, and make all members `virtual` / `abstract`.

Hope this helps.
Regards,
David




--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

tasos foobar

unread,
Jun 3, 2015, 7:54:35 PM6/3/15
to nsubs...@googlegroups.com
Thank you very much David!
Much appreciated!
Reply all
Reply to author
Forward
0 new messages