Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

September 2010 - Posts

Mocking SPContext.Current with JustMock

Today, I happen to find an interesting post on mocking SharePoint context using TypeMock. Being a JustMocker, i thought rather to follow the footsteps and see if can do the same with JustMock. I am no SharePoint expert. Occasionally, I use a Windows 2003 VM with SharePoint server installed which gives me the required flavor of SharePoint for testing  SP capabilities of JM.

Anyway, here is the original post that i am going to recycle using JustMock.

http://meronymy.blogspot.com/2010/09/mocking-spcontextcurrent-with-typemock.html

 

The scenario here is pretty simple. There is a method that returns an URL from the current SPContext which i am going to mock in order to return my expected URL.

 

Therefore, the class for which the behavior to be mocked is similar to:

  1. public class Site
  2. {
  3.     public static string GetHomePageUrl()
  4.     {
  5.         return SPContext.Current.Site.Url;
  6.     }
  7. }

 

Finally, the test that creates fake instance of SPSite and passes it to the current context:

  1. [TestMethod]
  2. public void ShouldAssertSPSiteUrlWithExpected()
  3. {
  4.     var fakeSiteUrl = "http://www.telerik.com";
  5.     var fakeSharepointSite = Mock.Create<SPSite>();
  6.  
  7.     // Arrange
  8.     Mock.Arrange(() => SPContext.Current.Site).Returns(fakeSharepointSite);
  9.     Mock.Arrange(() => fakeSharepointSite.Url).Returns(fakeSiteUrl);
  10.  
  11.     // Act
  12.     string actualUrl = Site.GetHomePageUrl();
  13.  
  14.     Assert.AreEqual(fakeSiteUrl, actualUrl);
  15. }
 

I have used  JustMock SP1 for the purpose and thanks to the original author for this sample.

 

Hope that helps

Posted: Sep 27 2010, 07:42 PM by mehfuzh | with no comments |
Filed under: , ,
More Posts