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:
is the following a correct JustMock equiv?
and another:
Right? Wrong?
Thank you for your time.
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; |
} |
Thank you for your time.