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:
- Why is the first approach to chaining/recursive calls failing?
- 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