Saturday, September 6, 2008

Test Case for a Feature receiver

Scenario:
You want to write a test case for the Feature Receiver

Sample Feature Code:

namespace TrainingSamples
{
public class SampleFeatureReceiver:SPFeatureReceiver
{

[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties == null)
{
return;
}

SPWeb web = (SPWeb)properties.Feature.Parent;
this.OnActivationCode(web);
}

public void OnActivationCode(SPWeb web)
{
//Code

}
}
}
Sample Test Case Code:
namespace TrainingSamples
{
[TestMethod()]
public void FeatureRecieverOnActivationTest()
{
SampleFeatureReceiver target = new SampleFeatureReceiver();

SPSite site;
SPWeb web;
//Bind to the site where you are testing provisioning code.

site = new SPSite("http://servername");
using (site)
{
web = site.RootWeb;
using (web)
{
target.OnActivationCode(web);
}
}

Assert.IsTrue(true,
"Feature Activation did not throw any exceptions.");
}
}
}

0 comments: