This is a migrated thread and some comments may be shown as answers.

HttpContext Mocking

3 Answers 230 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 11 Jan 2011, 09:32 PM
I'm trying to test some web aware code using HttpContext.Current (Request & Server).

JustMock is supposed to support this, but the working example is very simple and not what I want to achieve.

What I Want to mock is

if (HttpContext.Current != null)
{
string s = HttpContext.Current.Request....
string t = HttpContext.Current.Server....
}

Can Justmock do this very simple task (I'm using the professional version)?.

Thanks

Ian

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 12 Jan 2011, 12:29 PM
Hello Ian,
Thanks for making the post. In regard to your case above, I  created a test method that returns custom request object from the mocked context and finally asserts it.

Accordingly, I have a target method:

private HttpRequest GetCurrentRequest()
{
    if (HttpContext.Current != null)
    {
        return HttpContext.Current.Request;
    }
    return null;
}


Then wrote the test method in the following way:

[TestMethod]
 public void TestMethod1()
 {
     var request = new HttpRequest("/", "http://telerik.com", "q=1");
     Mock.Arrange(() => HttpContext.Current.Request).Returns(request);
     Assert.AreEqual(GetCurrentRequest(), request);
     Mock.Assert(() => HttpContext.Current.Request);
}

Since, Request is a nested property it additionally illustrates the nested mocking capabilities of JustMock that can save you additional lines.

Here to mention that to mock HttpContext and its members you need to add additional MockedClassAttribute on top your test class as like mocking mscorlib types.

Finally, I attached the test project to also let you have a look and please do feel free to write back for any issues.


Kind Regards,
Ricky
the Telerik team
Browse the videos here>> to help you get started with JustMock
0
Ian
Top achievements
Rank 1
answered on 12 Jan 2011, 06:06 PM
Ricky,

many thanks for the reply, especially the working example, which works, but it doesnt work in my context, its probably me being stupid..

The class I'm testing has the following constructor:
public OriginatingRequest()
{
    if (HttpContext.Current != null)
    {

My test code calls this (using your example)

 

var request = new HttpRequest("/", "<A href="http://telerik.com">http://telerik.com<;/A>", "q=1");
  
Mock.Arrange(() => HttpContext.Current.Request).Returns(request);
   
OriginatingRequest target = new OriginatingRequest();

The check for HttpContext.Current != null fails, I dont know why, I was having the same issue before, I thought that MsCorLib classes would be picked up by my test class.

PS I am setting the MockClass attribute on my test class, do I need to do it on the target class too?

Regards

Ian


0
Ricky
Telerik team
answered on 14 Jan 2011, 11:52 AM
Hi Ian,
Thanks again for reporting the issue. Actually, this is a nice catch and HttpContext.Current do failing when wrapped around a class. However, if I apply a MockedClassAttribute on the target, the test passes. In case of third-party classes this is not possible at any chance.

Therefore, i am adding this as a bug which you can track at:
http://www.telerik.com/support/pits.aspx#/public/justmock/4614

Finally, sorry for the inconvenience and please do feel free to write us for any further issues.


Kind Regards,
Ricky
the Telerik team
Browse the videos here>> to help you get started with JustMock
Tags
General Discussions
Asked by
Ian
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Ian
Top achievements
Rank 1
Share this question
or