In ServiceStack can I make fun of the Request.OriginalRequest object for unit tests?

I want my ServiceStack service to pass validation.

I currently have:

[RequireFormsAuthentication] public object Delete(DeleteRequest request) { var originalRequest = (HttpRequest)Request.OriginalRequest; var identity = originalRequest.RequestContext.HttpContext.User.Identity; return othercode(identity); } 

Where RequireFormsAuthentication

 public class RequireFormsAuthenticationAttribute : RequestFilterAttribute { public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto) { var originalRequest = (HttpRequest)req.OriginalRequest; var identity = originalRequest.RequestContext.HttpContext.User.Identity; if (!identity.IsAuthenticated) { res.StatusCode = (int)HttpStatusCode.Forbidden; res.EndServiceStackRequest(skipHeaders: true); } } } 

I mocked all the dependencies used by "othercode ()", and all that was left was material that is in the base class of Service. Is there a template / strategy / approach / something that I am missing that makes this trivial?

+6
source share
3 answers

I apologize for not using moq ... some of this has already been done using RhinoMocks. I think the concept should go to moq. This one can be a good resource as well.

In any case, I think the test code below should get you started. Your mockery Request.OriginalRequest mock replaces Service.RequestContext mock object. Then you just need to mock all of this. It will be a lot of "ridicule", and if you repeat to yourself, "Are you kidding me" every time you mock the class, it is almost nice.

 [Test] public void testsomethign() { var mockedRequestContext = MockRepository.GenerateMock<IRequestContext>(); var mockedHttpRequest = MockRepository.GenerateMock<IHttpRequest>(); var mockedOriginalRequest = MockRepository.GenerateMock<HttpRequestBase>(); var mockedOriginalRequestContext = MockRepository.GenerateMock<RequestContext>(); mockedOriginalRequest.Stub(x => x.RequestContext).Return(mockedOriginalRequestContext); mockedHttpRequest.Stub(x => x.OriginalRequest).Return(mockedOriginalRequest); mockedRequestContext.Stub(x => x.Get<IHttpRequest>()).Return(mockedHttpRequest); var service = new ServiceTests() { RequestContext = mockedRequestContext }; service.Delete(new DeleteRequest()); } 
+2
source

Here's how to test with Moq. This service looks for "key" and "value" in the query string and another parameter in the DTO request. The service returns a string response based on the specified value.

  [Test] public void MyTest() { var mockRequestContext = new Mock<IRequestContext>(); var mockedHttpRequest = new Mock<IHttpRequest>(); NameValueCollection querystring = new NameValueCollection(); querystring.Add("myKey", "myValue"); mockedHttpRequest.SetupGet(r => r.QueryString).Returns(querystring); mockRequestContext.Setup(x => x.Get<IHttpRequest>()).Returns(mockedHttpRequest.Object); AboutService service = new AboutService { RequestContext = mockRequestContext.Object, }; AboutResponse response = (AboutResponse)service.Any(new About { Company = "myOtherValue", }); Assert.AreEqual(0, response.QueryResult); Assert.AreEqual("validResponse", response.Version); } 
+7
source

Be sure to check the namespace: ServiceStack.ServiceInterface.Testing.
There you can find the MockRequestContext, which you can use as follows:

 var mockContext = new ServiceStack.ServiceInterface.Testing.MockRequestContext(); //do stuff to set it up if desired... AboutService service = new AboutService { RequestContext = mockContext }; 
+2
source

Source: https://habr.com/ru/post/943631/


All Articles