Scenario:
You want to test the webpart code in sharepoint context
Solution:
You can use the Visual Studio Test classes. Code sample shows both using Context and without context.
Code(Without Context):
public static string GetSiteTitle(string url) {Code(With Context):
SPSite site = new SPSite(url);
string title = site.RootWeb.Title;
site.RootWeb.Dispose();
site.Dispose();
return title;
}
[TestMethod()]
public void GetSiteTitleTest() {
string url = http://localhost;
string expected = "Home";
string actual = MyClass.GetSiteTitle(url);
Assert.AreEqual(expected, actual);
}
public static string GetSiteTitle() {Article:
SPSite site = SPContext.Current.Site;
string title = site.RootWeb.Title;
return title;
}
[TestMethod()]
[HostType("ASP.Net")]
[UrlToTest("http://localhost/default.aspx")]
public void GetSiteTitleTest() {
string expected = "Home";
string actual = MyClassName.GetSiteTitle();
Assert.AreEqual(expected, actual);
}
http://blogs.msdn.com/mcsnoiwb/archive/2008/10/08/unit-testing-sharepoint-code.aspx
0 comments:
Post a Comment