Pex And Dynamic Test Methods

Pex as covered in past posts is a pretty great white box testing tool. But let’s assume that you want to have a “factory” in the sense that test methods can be automagically built out. An example of this is you have a list of some acceptable data type, and you want harness a test case for each one, and avoid the use of haphazard data keeping in mind that Pex will only use varying values.

In this case, you must push Pex to use parameterized unit tests since you will essentially be targeting a value from a list of values. In the below, I am assuming an array of integers is being populated by the void PopulateIntArray() method. Following, the Test method is defined which has the method decoration of PexMethod which implies it is Pex parameterized unit test. In the method, we are leveraging the PexAssume.IsTrue which means the test case fails if the condition evaluates to false.

[csharp]
static int[] intArray = PopulateIntArray();
[PexMethod]
public void Test(int index)
{
PexAssume.IsTrue(index >= 0 && index < intArray.Length); int x = intArray[index]; ... } [/csharp] EDIT: As Peli points out in the comments, you can use the PexArguments decoration to pass in values as well.

Share

Protected Constructors For Abstract Classes

One code modification Resharper 4.0 will suggest is that if a public constructor is located in an abstract class, it should be adjusted to use the protected access modifier if currently public:

So, from this:

[csharp]
public abstract class AbstractClass
{
public AbstractClass()
{
}
}
[/csharp]

To this:

[csharp]
public abstract class AbstractClass
{
protected AbstractClass()
{
}
}
[/csharp]
So, why is this a good code edit?

Well, having a public constructor on an abstract class in the terms of code architecture is irrational since the abstract class can’t be instantiated directly; it is instead created by instantiating the deriving type. Thus only derived types have access to the abstract class constructor. Protected more adequately describes a germane access modifier for the constructor as well as ensuring requisite inheritor types have constructor access.

More noticeably, appropriate access modifier decoration will in turn not populate Intellisence with a bunch of useless crap :)

Share

Building SharePoint View Queries with CAML

Often times when building applications that will greatly use SharePoint views for data rotation it is helpful to build CAML builders into separate static classes that return a continuous string representation of the arbitrary query. In order to demonstrate this, consider the following examples BuildDefaultViewQuery, BuildPrivateViewQuery, and BuildPublicViewQuery:
[csharp]
private static string BuildDefaultViewQuery()
{
var builder = new StringBuilder();
builder.Append(““);
builder.Append(““);
builder.Append(“1”);
builder.Append(“
“);
return builder.ToString();
}

private static string BuildPrivateViewQuery(string user)
{
var builder = new StringBuilder();
builder.Append(“ “);
builder.Append(““);
builder.Append(user);
builder.Append(“
“);
return builder.ToString();
}

private static string BuildPublicViewQuery(string user)
{
var builder = new StringBuilder();
builder.Append(“ “);
builder.Append(““);
builder.Append(user);
builder.Append(“
“);
builder.Append(““);
builder.Append(“1”);
builder.Append(“
“);
return builder.ToString();
}
[/csharp]
It should be noted that there are some constants that could instead be extracted to an enumeration and then Enum.GetName could be used for better component allocation. However, in this example since it is merely to show the overlying constructs, is kept purposefully plain.

In order to use one of static CAML methods, the ReturnCollectionExample method will take some basic parameters, than return the item collection associated with the BuildPrivateViewQuery query.

[csharp]
private static SPListItemCollection (string url, string viewName, string userId)
{
using (var site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
var query = new SPQuery { Query = BuildPrivateViewQuery(userId) };
return web.Lists[viewName].GetItems(query);
}
}
}

[/csharp]

Share