JustMock is still having issues faking SharePoint objects

6 posts, 0 answers
  1. Mike
    Mike avatar
    5 posts
    Member since:
    Oct 2007

    Posted 05 May 2010 Link to this post



    Hi There,

    I ran into a few issues when I attempted to fake various SharePoint objects with the first Beta release of JustMock and am experiencing some different errors with the 2010.1.429 build.

    I first tried a few simple tests (as seen below) and they worked as expected:
    [TestMethod] 
    [ExpectedException(typeof(ApplicationException))] 
    public void Current_NotAvailable_ThrowsApplicationException() 
        // Arrange 
        Mock.Arrange(() => SPContext.Current).Throws(new ApplicationException("Not allowed to call SPContext.Current")); 
     
        // Act 
        SPContext currentContext = SPContext.Current; 
     
    [TestMethod] 
    public void Current_CurrentContextIsFake_ReturnsFakeSPContext() 
        // Arrange 
        var fakeContext = Mock.Create<SPContext>(); 
        Mock.Arrange(() => SPContext.Current).Returns(fakeContext); 
     
        // Act 
        SPContext currentContext = SPContext.Current; 
     
        // Assert 
        Assert.IsNotNull(currentContext, "The current SPContext should not be null"); 

    I then wanted to try some of the chaining behavior, as this is needed to work with the SPContext object, so I followed your ShouldAssertNestedPropertySetups example

    [TestMethod] 
    public void ShouldAssertNestedPropertySetups() 
        var foo = Mock.Create<IFoo>(); 
         
        Mock.Arrange(() => foo.Bar.Value).Returns(10); 
     
        Assert.Equal(10, foo.Bar.Value); 

    and created the following test

    [TestMethod] 
    public void SPWeb_AllowAnonymousAccess_ReturnsTrue() 
        // Arrange 
        var fakeContext = Mock.Create<SPContext>(); 
        Mock.Arrange(() => fakeContext.Web.AllowAnonymousAccess).Returns(true); 
     
        // Assert 
        Assert.IsTrue(fakeContext.Web.AllowAnonymousAccess, "Our SPWeb should allow anonymous access"); 

    which pegs my CPU, takes 15 seconds to run, and fails with the following information:

    Error Message:
    Test method JustMockSharePointSamples.ComponentsTests.SPContextTests.SPWeb_AllowAnonymousAccess_ReturnsTrue threw exception:  System.NullReferenceException: Object reference not set to an instance of an object..

    Error Stack Trace:
    Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
    Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context)
    Microsoft.SharePoint.SPContext.get_Web()
    lambda_method(ExecutionScope )
    Intercept(Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs: line 72
    TReturn](† instruction, Func2`2 function) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs: line 173
    Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs: line 37
    JustMockSharePointSamples.ComponentsTests.SPContextTests.SPWeb_AllowAnonymousAccess_ReturnsTrue() in C:\Projects\JustMockSharePointSamples\JustMockSharePointSamples.ComponentsTests\SPContextTests.cs: line 94

    My Visual Studio 2008 (SP 1) Test Project is referencing the following libraries:
    • Microsoft.Office.Server
    • Microsoft.SharePoint
    • Microsoft.VisualStudio.QualityTools.UnitTestFramework
    • System
    • System.Core
    • System.Data
    • System.Web
    • System.XML
    • Telerik.JustMock

    I then tried the following approach

    [TestMethod] 
    public void CurrentWeb_AllowAnonymousAccess_ReturnsTrue() 
        // Arrange 
        var fakeContext = Mock.Create<SPContext>(); 
        Mock.Arrange(() => SPContext.Current).Returns(fakeContext); 
     
        var fakeWeb = Mock.Create<SPWeb>(); 
        Mock.Arrange(() => fakeContext.Web).Returns(fakeWeb); 
        Mock.Arrange(() => fakeWeb.AllowAnonymousAccess).Returns(true); 
     
        // Assert 
        Assert.IsTrue(SPContext.Current.Web.AllowAnonymousAccess, "Anonymous access should be allowed on our current SPWeb"); 

    and this test passes ... but my CPU pegs once again, and the test takes just over 40 seconds to run.

    So, my questions are:
    1. Why is the first approach to chaining/recursive calls failing?
    2. Why is the test pegging my CPU and taking so long? When I run something similar with Typemock, it completes within seconds.

    If you require any additional information, please let me know.

    Thanks.
    Mike
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 May 2010 Link to this post

    Hi Mike,
    Thanks for the post and few nice samples,  The CPU load could happen due to the number of  methods that are in the SPContext class as Mock.Create<> makes the whole class as mocakble.

    One, of the solution to this high load class[that has more static and concrete stuffs and less virtual and interface stuff] is to partially mock the members. Like with your sample:

    [TestMethod]  
    public void CurrentWeb_AllowAnonymousAccess_ReturnsTrue()  
    {  
        // Arrange  
        var context = new SpContext();
        Mock.Arrange(() => context.Current).Returns(fakeContext);  
        
        var fakeWeb = Mock.Create<SPWeb>();  
        Mock.Arrange(() => context.Web).Returns(fakeWeb);  
        Mock.Arrange(() => fakeWeb.AllowAnonymousAccess).Returns(true);  
        
        // Assert  
        Assert.IsTrue(SPContext.Current.Web.AllowAnonymousAccess, "Anonymous access should be allowed on our current SPWeb");  
    }  


    Hope that helps,
    Mehfuz

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
  4. Mike
    Mike avatar
    5 posts
    Member since:
    Oct 2007

    Posted 11 May 2010 Link to this post

    Hi Mehfuz,

    Two issues:

    1. SPContext does not contain a public constructor, this is one of the reasons why I want to create a fake one
    2. The chaining of property calls isn't working (as I mentioned with the stack trace I provided). I should be able to call something like

    Mock.Arrange(() => SPContext.Current.Web.AllowAnonymousAccess).Returns(true); 

    but this blows up with a null reference exception (as I noted in my original post). When I try something like the snippet I included above using Typemock, it runs very quickly and it works - I'd love to see JustMock be able to do the same thing.

    Thanks in advance.
    Mike
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 May 2010 Link to this post

    Hi Mike ,
    Thanks for the post. One thing i would like to mention that you can instantiate classes even they dont have public constructor using Mock.Create<>. Secondly,  I will take a look at the chaining issue. Finally, will it be possible to give me a sample test project, so that i can debug the extact scenario [it will be nice].

    Thanks in advance,
    Mehfuz


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Mike
    Mike avatar
    5 posts
    Member since:
    Oct 2007

    Posted 12 May 2010 Link to this post

    Hi Mehfuz,

    In my original example I did use Mock.Create<SPContext> to create a fake instance of that class since it doesn't have a public constructor, but as I indicated the tests pegged my CPU and took quite a while to run (especially when I had to create an instance of SPWeb).

    I have a sample project that I can send you ... what is the best way of getting it to you (the attachments in this forum are only for pictures). It would be great if you can use my sample to figure out why it pegs the CPU and takes significantly longer than Typemock to carry out the same actions (from a testing perspective) as well as the chaining issue.

    I look forward to your response.

    Cheers.
    Mike
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 14 May 2010 Link to this post

    Hi Mike,

    You can directly contact me  at mehfuz.hossain@telerik.com to send anything you like regarding JustMock. Also, thanks in advance for sending your project.


    Regards,
    Mehfuz



    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top
DevCraft R3 2016 release webinar banner