I thought of posting a bit of code to develop Unit testable work item. Let us call this UnitTestWorkItem. The following class can act as a root work item to which you can add other work items, views, services etc.
public class UnitTestWorkItem : WorkItem
{
public UnitTestWorkItem()
{
InitializeRootWorkItem(CreateBuilder());
Services.AddNew
Services.AddNew
}
public Builder Builder
{
get { return InnerBuilder; }
}
public IReadWriteLocator Locator
{
get { return InnerLocator; }
}
private Builder CreateBuilder()
{
Builder builder = new Builder();
builder.Strategies.AddNew
builder.Strategies.AddNew
builder.Strategies.AddNew
builder.Strategies.AddNew
builder.Strategies.AddNew
builder.Strategies.AddNew
builder.Policies.SetDefault
builder.Policies.SetDefault
return builder;
}
}
The above code will create the necesasry infrastructure to create the foundation to unit test your CAB applications. Here is a peice of sample code that instatiate a WorkItem and then gets it ready to test.
private CreateBankTellerView target;
private UnitTestWorkItem rootWorkItem;
private TellerWorkItem WiTeller;
rootWorkItem = new UnitTestWorkItem ();
WiTeller= rootWorkItem.WorkItems.AddNew
WiTeller.Services.AddNew
target = WiCust.SmartParts.AddNew
The above code create the views, services and work items that need testing. However, to pass values to it and invoke some events you need to do a lot more. But here is the trick, I have used the VS.NET 2005 testing project that generate the accessor class for you. And that makes it easy to test the views.
CreateBankTellerViewAccessor accessor = new CreateBankTellerView(target);
accessor.txtFirstName = "John";
accessor.btnSave_Click(null,null);
In the above code line is firing the button click event. This is a good starting point to unit test CAB apps.
See you untill my next post. You can find similar code from the GotDotNet project portal. The code above has been adapted from the same site.