kamagra how much to take

Working Around BehaviorNotImplementedException When Instantiating Stubs

I got this exception in this morning, but thankfully there is a workaround. Do not despair!

Assume the following code. In the Concrete class we have a global level _internalString field and the virtual TestString property with returning the string literal “Pex Is Pissing Adam Off!” (as it was this morning!) in the Concrete base class constructor we are simply backing field settings, nothing amazing. Yeah the virtual member call in the concrete class constructor is bad since it will cause the most derived override to be called, even though the most derived constructor has not been fully run yet (may not be in a suitable state to have that method called). But bah! It’s part of the code and it’s not like that warning isn’t already thrown in nearly every SharePoint artifact.

Anyways, so in the rest of the code there is nothing amazing that is happening, as you can see the class containment hierarchy is using the Concrete base class for the child Superclass derived class, which is just overriding a simple property.

  1. namespace AdamHatingPex
  2. {
  3. public class Concrete
  4. {
  5. private string _internalString;
  6. public virtual string TestString{ get { return "Pex Is Pissing Adam Off!"; } }
  7. public Concrete()
  8. {
  9. _internalString = TestString;
  10. }
  11. }
  12. public class SuperClass : Concrete
  13. {
  14. public override string TestString
  15. {
  16. get { return "But I'll Get Over It"; }
  17. }
  18. }
  19. }

Assuming this code, if you stub out the derived class you are going to get a Microsoft.Moles.Framework.Behaviors.BehaviorNotImplementedException error, as you can guess since it’s the only thing in the class it occurs on the overridden property. Quick hint, using CallBase with your call won’t help since base constructors should be run before derived ones (going back to what was discussed previously).
However, it is solvable by changing the behavior. I am not going to get into behaviors yet but you can adjust behavior pre-instantiation of the stub and let it Fall Through or Default Provide A Value. By default the instance will use the BehavedBehaviors.Current property, which will by default throw a BehavedBehaviors.NotImplemented. You can switch this around by setting the InstanceBehavior property on any stub instance. This is pretty easy, demonstrated below:

  1. BehavedBehaviors.Current = BehavedBehaviors.Fallthrough;
  2. var x = new Test.Moles.SSuperClass();

When you are done with your stuff, you can return to default behavior:

  1. BehavedBehaviors.Current = BehavedBehaviors.NotImplemented;

If you are interested in this, you should look into the [assembly: PexChooseAsBehavedBehavior] attribute further as well.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>