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.

[csharp]
namespace AdamHatingPex
{
public class Concrete
{
private string _internalString;
public virtual string TestString{ get { return “Pex Is Pissing Adam Off!”; } }
public Concrete()
{
_internalString = TestString;
}
}
public class SuperClass : Concrete
{
public override string TestString
{
get { return “But I’ll Get Over It”; }
}
}
}
[/csharp]

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:

[csharp]
BehavedBehaviors.Current = BehavedBehaviors.Fallthrough;
var x = new Test.Moles.SSuperClass();
[/csharp]

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

[csharp]
BehavedBehaviors.Current = BehavedBehaviors.NotImplemented;
[/csharp]

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

Share

Moles On Base Classes

This one took me a little bit to understand. Sometimes the Moles exception handling is so aggressive that it becomes a little tricky to understand exactly what is bubbling up.
Assume MethodX() is a member of base class ConcreteX. Requirements state that you need to have moles for MethodX() in order to support data mocking. It is important to remember for this particular requirement that base member access is achieved by allocating the mole type of ConcreteX and passing the instance in the constructor. This looks like the following:
[csharp]
ConcreteX x = new ConcreteX();
new MObjectContenxt(x).
[/csharp]
Now, if it isn’t done in this fashion MethodX() will not be moled, however you can still mole the entire assembly I suppose. However, doing it in the above fashion avoids using a wrapper in partial classes and a lot of rework based on class hierarchy.

Share

Programatically Working With TFS Check-In Policies

Check-in policies within mature development environments are imperative, allowing the construction of code, to police checking in of code. Policies within TFS aid in enforcing restrictions and limitations whenever files are checked into TFS VC. TFS supplies a multitude of pre-existing check-in policies for actions like checking that unit tests are implemented, executing static code analysis to check for standards, and a bunch of others.

Unfortunately, there are some limitations however which I ran into recently with a current client of mine. The long and short of it is check-in policies cannot be included in process templates. However, there are custom development avenues you can use by building some managed code against the TFS API to execute this, the objects and methods that are used are fairly self explanatory. Consider the following that shows getting a collection of the current policies, and then setting the policies.

Firstly, getting an appropriate reference to the desired project:

[csharp]
TeamFoundationServer tfs = new TeamFoundationServer(http://tfs:8080);
VersionControlServer vcs = (VersionControlServer) tfs.GetService(typeof(VersionControlServer));
TeamProject tp = vcs.GetTeamProject(“Project”);
[/csharp]

Off the TeamProject object instance, you can following call the GetCheckinPolicies method, which will allow you to return a Microsoft.TeamFoundation.VersionControl.Client.PolicyEnvelope array type. Essentially this is what we can refer to as the policy instance, representing the applied policy’s of type IPolicyDefinition. Since you have an active array, you can do push the array into a typed collection of PolicyEnvelope objects using List.AddRange

[csharp]
tp.SetCheckinPolicies(policyCollection.ToArray());
[/csharp]

Share