HttpContext currentContext = Mock.Create<HttpContext>(Constructor.Mocked);
HttpContext currentContext = Mock.Create<HttpContext>();
Object reference not set to an instance of an object.
These are the two errors I am getting. My Question is what is the current way to mock HttpContext constructor.
I am using JustMock Trial Version Q2 2011 (2011.2.713.2)
Thanks
9 Answers, 1 is accepted
Thanks for reporting the issue. However, ambiguous exception during Mock.Create<HttpContext> using Constructor.Mocked is now fixed and will be available in the coming Q3 build this week.
On your second issue, the null reference exception thrown by the constructor itself for the required arguments. In that regard you can also try the method shown in the following example to mock HttpContext class:
var expectedException =
new
ArgumentException();
var request =
new
HttpRequest(
string
.Empty,
"http://telerik.com"
,
null
);
var response =
new
HttpResponse(
null
);
HttpContext context = Mock.Create<HttpContext>(() =>
new
HttpContext(request, response));
bool
called =
false
;
Mock.Arrange(() => context.Error).DoInstead(() => called =
true
).Returns(expectedException);
Assert.Equal(context.Request, request);
Assert.Equal(context.Error, expectedException);
Assert.True(called);
Kind regards,
Mehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I dont know what it is with HttpContext.Current. But it's just not happening. I tried both with Constructor.Mocked and with my own request, response object. The creation step does not throw an exception, but HttpContext.Current is still null.
//var currentContext = Mock.Create<HttpContext>(Constructor.Mocked);
var httpResponse =
new
HttpResponse(
null
);
var httpRequest =
new
HttpRequest(
string
.Empty,
"http://foo.com/"
,
null
);
var currentContext = Mock.Create<HttpContext>(() =>
new
HttpContext(httpRequest, httpResponse));
Mock.SetupStatic<HttpContext>();
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
Please take another look, thanks.
Thanks again for posting the issue. However HttpContext is a special class like mscorlib , therefore please make sure that you have applied [MockClass] attribute on top of the test class.
With the attribute in place, i tried the following and it worked as expected:
var httpResponse =
new
HttpResponse(
null
);
var httpRequest =
new
HttpRequest(
string
.Empty,
"http://foo.com/"
,
null
);
var currentContext = Mock.Create<HttpContext>(() =>
new
HttpContext(httpRequest, httpResponse));
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
Assert.IsNotNull(HttpContext.Current);
Here to mention that you generally dont require the Mock.SetupStatic<HttpContext>() unless you are doing strict mocking or want to fake the static constructor.
Moreover, I have used the latest Q3 build for testing out the problem.
Kind Regards,
Mehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
[TestClass]
[MockClass]
public
class
JustMockTest1
{
[TestMethod]
public
void
GetAbsoluteUri()
{
//var currentContext = Mock.Create<HttpContext>(Constructor.Mocked);
var httpResponse =
new
HttpResponse(
null
);
var httpRequest =
new
HttpRequest(
string
.Empty,
"http://lms.auckland.ac.nz/"
,
null
);
var currentContext = Mock.Create<HttpContext>(() =>
new
HttpContext(httpRequest, httpResponse));
//Mock.SetupStatic<HttpContext>();
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
//Mock.Arrange(() => HttpContext.Current.Request).Returns(httpRequest);
Assert.IsNotNull(HttpContext.Current);
Assert.IsNotNull(HttpContext.Current.Request);
//Exception has been thrown by the target of an invocation.
Mock.Arrange(() => HttpContext.Current.Request.Url.AbsoluteUri).Returns(
"http://lms.auckland.ac.nz/default.aspx"
);
var str = HttpContext.Current.Request.Url.AbsoluteUri;
//Object reference not set to an instance of an object.
Assert.Equals(str,
"http://lms.auckland.ac.nz/default.aspx"
);
}
}
Thanks again for the code sample. However, you can write the test in the following way that works as expected:
var httpResponse =
new
HttpResponse(
null
);
var httpRequest =
new
HttpRequest(
string
.Empty,
"http://lms.auckland.ac.nz/"
,
null
);
var currentContext = Mock.Create<HttpContext>(() =>
new
HttpContext(httpRequest, httpResponse));
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
Assert.IsNotNull(HttpContext.Current);
Assert.IsNotNull(HttpContext.Current.Request);
var request = HttpContext.Current.Request;
Mock.Arrange(() =>request.Url.AbsoluteUri).Returns(
"http://lms.auckland.ac.nz/default.aspx"
);
var str = HttpContext.Current.Request.Url.AbsoluteUri;
//Object reference not set to an instance of an object.
Assert.Equals(str,
"http://lms.auckland.ac.nz/default.aspx"
);
Here in the following line:
var currentContext = Mock.Create<HttpContext>(() => new HttpContext(httpRequest, httpResponse));
You are already setting the request object , therefore you can either write the test as above or can add an extra line:
Mock.Arrange(()=> HttpContext.Current.Request).CallOriginal();
However, I found that the above line is producing an exception similar to your one, therefore will include the fix in the service pack release or if you can create a ticket then i can send you an internal build.
Kind regards,
Mehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Bascially I have two project, Project A the actual sharePoint projec, Project B the unit testing project
The mocking for HttpContext.Current is setup in Test Method, then I call the actual code in Project A, which will make use of HttpContext.Current. And that returns null.
Thanks
Thanks again for reporting the issue. Yes you are right that there is a bug with HttpContext.Current when mocked from outside the scope of the test class. This is due to the MockClassAtrribute usage bug. If you apply MockClassAttribute on top of the class from Project A, you will see that it will work as expected.
However, we are working on this and a fix will be available in the Q3 SP1 shortly.
Kind Regards,
Mehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Here is my sample code, please take a look.
In it I have a code class (Class1.cs), my actual code that I want to unit test, it queries the httpRequest for a query string value.
In my unit test code (Test.cs), I want to mock the query string return value.
TestCase1 which is just plain mocking the HttpContext.Current. And that also give me the NullReferenceException.
Class1.cs
namespace
HttpContextTest
{
using
Telerik.JustMock;
[MockClass]
public
static
class
CodeClass
{
public
static
string
AMethod()
{
return
Utility.CurrentHttpContext.Invoke().Request[Utility.QueryStringCourseGroupKey];
// NullReferenceException
}
}
}
Utility.cs
namespace
HttpContextTest
{
using
System;
using
System.Web;
using
Telerik.JustMock;
[MockClass]
public
static
class
Utility
{
public
const
string
QueryStringCourseGroupKey =
"courseGroup"
;
public
static
Func<HttpContext> CurrentHttpContext {
get
;
set
; }
public
static
void
Reset()
{
CurrentHttpContext = () => HttpContext.Current;
}
}
}
Test.cs
namespace
HttpContextTest
{
using
System.Web;
using
NUnit.Framework;
using
Telerik.JustMock;
[TestFixture, MockClass]
public
class
Test
{
[Test]
[TestCase(
"current"
)]
public
void
TestCase(
string
expected)
{
// Setup
var httpResponse = Mock.Create<HttpResponse>();
var httpRequest = Mock.Create<HttpRequest>(Constructor.Mocked);
var httpContext = Mock.Create<HttpContext>(Constructor.Mocked);
Mock.Arrange(() => httpContext.Request).Returns(httpRequest);
Mock.Arrange(() => httpContext.Response).Returns(httpResponse);
Mock.Arrange(() => HttpContext.Current).Returns(httpContext);
Mock.Arrange(() => httpRequest[Utility.QueryStringCourseGroupKey]).Returns(expected);
// action
Utility.Reset();
var actual = CodeClass.AMethod();
// assert
Assert.AreEqual(actual, expected);
}
[Test]
[TestCase(
"current"
)]
public
void
TestCase1(
string
expected)
{
var httpResponse = Mock.Create<HttpResponse>();
var httpRequest = Mock.Create<HttpRequest>(Constructor.Mocked);
var httpContext = Mock.Create<HttpContext>(Constructor.Mocked);
Mock.Arrange(() => httpContext.Request).Returns(httpRequest);
Mock.Arrange(() => httpContext.Response).Returns(httpResponse);
Mock.Arrange(() => HttpContext.Current).Returns(httpContext);
//Mock.Arrange(() => HttpContext.Current.Request).Returns(httpRequest);
Mock.Arrange(() => httpRequest[Utility.QueryStringCourseGroupKey]).Returns(expected);
var actual = HttpContext.Current.Request[Utility.QueryStringCourseGroupKey];
Assert.AreEqual(actual, expected);
}
}
}
Thanks
Thanks again for reporting the issue. However, I found out that the first test is failing because HttpContext.Current is executed via Func delegate that is on the other hand not executing the appropriate mock setup and thus returning null. I am adding this as an issue which you can follow here:
http://www.telerik.com/support/pits.aspx#/public/justmock/10713
Sorry for the inconvenience.
Kind regards,
Mehfuz
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>