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

Trying to wrap my head around this

3 Answers 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Wade
Top achievements
Rank 1
Wade asked on 19 May 2010, 06:48 PM
Okay, I am quite new at UnitTesting and the Mocking thing so bear with me an my ignorance.
Since MVC is great for UnitTesting, I thought I would start to try to wrap my head around these concepts/practices for several reasons. 
So...I was looking at MOQ and then I remembered Telerik had a beta going of JustMock, So I decided to try JustMock first.

So, in delving into unit tests, mocking, and MVC, I came across the following link: http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx which has some Mvc http mocking helpers.  Which I want to ultimately try to use to test a controller doing a post.  Since my controller also looks/processes some info from the HttpRequestBase Controller.Request, I think I need to mock the request in my unit test (could be wrong, but makes sense). Anyway, after reading the above link, it became painfully obvious that different mock libraries use different syntax, making things all that more confusing. The link uses examples for Rhino, Moq, and TypeMock..all slightly different.. grrr.  I learn by examples, trying, so I am trying to equate what I see in the above link to how it needs to be in JustMock.

In their example (I'll just focus on the Moq, since that's my second choice for mocking and seems to be a good/acceptable mocking tool)
Moq:
public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod) 
    Mock.Get(request) 
    .Expect(req => req.HttpMethod) 
    .Returns(httpMethod); 
 
 
 
is the following a correct JustMock equiv?
public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod) 
    var test = Mock.Create<HttpRequestBase>(); 
    Mock.Arrange(() => test).Returns(request); 
    Mock.Arrange(() => test.HttpMethod).Returns(httpMethod); 
 

and another:
//Moq: 
public static HttpContextBase FakeHttpContext() 
    var context = new Mock<httpcontextbase>(); 
    var request = new Mock<httprequestbase>(); 
    var response = new Mock<httpresponsebase>(); 
    var session = new Mock<httpsessionstatebase>(); 
    var server = new Mock<httpserverutilitybase>(); 
 
    context.Expect(ctx => ctx.Request).Returns(request.Object); 
            context.Expect(ctx => ctx.Response).Returns(response.Object); 
    context.Expect(ctx => ctx.Session).Returns(session.Object); 
    context.Expect(ctx => ctx.Server).Returns(server.Object); 
 
    return context.Object; 
 
//my attempt to convert to JustMock: 
public static HttpContextBase FakeHttpContext() 
    var context = Mock.Create<HttpContextBase>(); 
    var request = Mock.Create<HttpRequestBase>(); 
    var response = Mock.Create<HttpResponseBase>(); 
    var session = Mock.Create<HttpSessionStateBase>(); 
    var server = Mock.Create<HttpServerUtilityBase>(); 
 
    Mock.Arrange(() => context.Request).Returns(request); 
    Mock.Arrange(() => context.Response).Returns(response); 
    Mock.Arrange(() => context.Session).Returns(session); 
    Mock.Arrange(() => context.Server).Returns(server); 
 
    return context; 
 
 
Right? Wrong?

Thank you for your time.


3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 20 May 2010, 11:49 AM
Hello Wade,

Thanks for the post. The following code snippet with moq :

public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)  
{  
    Mock.Get(request)  
    .Expect(req => req.HttpMethod)  
    .Returns(httpMethod);  
}  

Equals  to :

public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)
{
    Mock.Arrange(() => request.HttpMethod).Returns(httpMethod);
}


But your second pasted snippet is correct :-).


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.
0
Mihai
Top achievements
Rank 1
answered on 04 Mar 2011, 01:58 PM
Hi,

I am trying to properly instantiate an MVC controller so that I can test an action, but can't figure out how to do it. I peek ending up with null fields in the controller instance. Could you please paste a full working test (or link to one) for something like this?

Thanks,
Mihai
0
Ricky
Telerik team
answered on 09 Mar 2011, 10:56 AM
Hi Mihai,

Thanks again for making the post. A sample project that shows how to mock controller action is posted under the following thread:


http://www.telerik.com/community/forums/justmock/general-discussions/proper-mvc-controller-mocking.aspx

Please check it out and let us know if you are still having issues.


Kind regards,
Ricky
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
General Discussions
Asked by
Wade
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Mihai
Top achievements
Rank 1
Share this question
or