Freigeben über


Pex 0.11 Released: Delegates, Exception Trees, and Stubs

We just released Pex v0.11.40421.0, which you can download here. This release brings support for Delegates as Parameters, a new Exception Tree View, Stubbed Events, and Recursive Stubs. This also release fixes a blocking issue (incorrect registration of the Stubs Visual Studio Add-in), and it brings Pex in sync with the latest Code Contracts release.

Update: This release may still require a separate download of OpenMP dlls, which part of the vcredist.

Delegate As Parameters

Pex now understands Parameterized Unit Tests that take delegates as parameters. Pex will automatically generate a delegate instance and its behavior: if the delegate returns a value, Pex will generate a new symbolic value for it, track its use, and then generate different values depending on the program behavior. Let’s consider a simple method to illustrate this new feature. I wrote the following Parameterized Test method which takes a Func<int> delegate and throws an exception if the delegate returns a particular value:

clip_image002

When executing Pex on Test, Pex will generate a Func<int> which ‘asks’ Pex to choose the returned int (it uses PexChoose under the hood). Therefore, for each call to that delegate, Pex has the liberty to return a diferent value. Based on how it is used, Pex will generate different values to increase coverage. In this case, Pex ‘chooses’ to return 123 on the first call, which is exactly what we need to cover the exception branch. The following code is generated by Pex; it starts by setting up the desired values, and then it calls the parameterized unit test.

clip_image004

Pex Explorer: Exception Tree View

We have started to work on improving the experience when applying Pex to a large number of explorations. To this end, a new window called Pex Explorer will show various views over the events produced by Pex. The Exception Tree View provides the tree of exception types that Pex found. This is really helpful to quickly drill through the (really) bad exceptions first.

clip_image006

Pex Explorer: Contract Failures Tree View

If you are using Code Contracts, Pex also provides a specialized view to sort the contract failures kind.

clip_image008

Events in Stubs

Stubs now support events: the stubs simply expose the backing delegate fields (which hold the event delegate) as a public fields. As a result, one can clear, set, and invoke the event as any other member. Let’s see this with an example:

clip_image010

In order to raise the SomeEvent in a test, we would simply have to invoke the backing delegate field, which happens to have the same name as the event:

clip_image012

It’s that simple.

Recursive Stubs

Another common feature of mock/stub frameworks is to support nested invocation of members. Stubs now lets you recursively invoke property getters. Instead of assigning the property getter delegate, you can use new helper methods ending in ‘AsStub’ that take care of allocating the nested stub, assigning it to the property getter and returning it. Let’s see this with an example: assume we want to test the little snippet below.

clip_image014

In order to set up our stub, we would need to have IParent.Child return a stub of IChild, then set up IChild.Name to return ‘joe’. For property getters, Stubs generates a helper method that does just that:

clip_image016

The ChildGetAsStub call instantiated a SIChild instance, assigned to the Child property and returned it. It’s that simple.

Fixed Bugs

  • .Stubx fail to generate the stubs. We fixed a bug with the registration of Stubs in Visual Studio.

Breaking Changes

  • Microsoft.Stubs.dll has been renamed to Microsoft.Stubs.Framework.dll. The public Stubs type has been moved under the Microsoft.Stubs.Framework namespace.
  • The generated stub class names are built by prepending ‘S’ to the type name (‘IFoo’ –> ‘SIFoo’). In the previous version, the ‘I’ of interface name was trimmed which sometimes created name clashes.

Comments


Pex: cannot run tests for selected method

The selected method is not public and cannot be run by Pex.

OK  

even if i'm clicking on a public method.

  • Anonymous
    May 24, 2010
    Hi, I have an issue where raising an event on a stub is throwing a null reference exception. An extract of the inerface and the stub are below. INTERFACE public interface IClientSiteProvisioningUCView    {        string SiteDescription { get; }                event EventHandler CreateSiteRequested;    } STUB    //     Stub type of Equiniti.ESPPortal.Views.IClientSiteProvisioningUCView .    [StubClass(typeof(IClientSiteProvisioningUCView))]    [DebuggerNonUserCode]    [DebuggerDisplay("Stub = IClientSiteProvisioningUCView")]    public class SIClientSiteProvisioningUCView : StubBase, IClientSiteProvisioningUCView    {        // Summary:        //     Event CreateSiteRequested        public EventHandler CreateSiteRequestedEvent;        //        // Summary:        //     Sets the stub of Equiniti.ESPPortal.Views.IClientSiteProvisioningUCView.SiteDescription        public MolesDelegates.Func<string> SiteDescriptionGet;        .... Code ommited for clarity You can see the CreateSiteRequest backing delegate is in the stub, however I cannot see its implementation. I would expect to see something like event EventHandler IClientSiteProvisioningView. CreateSiteRequestedEvent { add { ...} remove {...} } this is as per the Microsoft Moles Reference Manual - 10, Version 0.91 – April 17, 2010 So essentially when I use the STUB above in my unit test, like the following:            var sClientSiteProvisionUC = new SIClientSiteProvisioningUCView();            sClientSiteProvisionUC.SiteDescriptionGet = () => "Test Site Description";            sClientSiteProvisionUC.CreateSiteRequestedEvent(sClientSiteProvisionUC, EventArgs.Empty); I get a null reference exception when trying to raise the event. Any help would be appreciated. Regards Elena