Saturday 15 January 2011

asp.net mvc 3 - How to mock two consecutive web requests -


I am trying to test a method that consistently two web requests.

Up until now I only work like a charm for single requests and it tests one single requests if you are not familiar about it, then can block the web request mechanism And works behind the scenes to return the expected response.

But the logic of my methods have been changed in this way (in Piudokod)

  public void MakePayment ( PaymentRequest request) {var statusRequest = webService.GetPaymentStatus (New PaymentStatusRequest () {OrderId = request.OrderId}); If (statusRequest.StatusId! = 100) {var responseXml = webService.GetResponse (GetRequestXml (request)); / * .. process response .. *}}   

Is there a way to handle two requests or is there any alternative option to test this option?

Here is an example of a work test:

  [test] public zero makeupment () {var response = " <.... ; / XML & gt; "; TestWebRequest Request = TestWebRequestCreate.CreateTestRequest (feedback); Var Results = webService.MakePayment (1, New PaymentRequest ()); Var Required Response = New Payment Response () {Status = 23}; Assurance First (expected receipt, result); }   

Now if MakePayment makes two requests, we need to duplicate two counterfeit reactions like this:

  [test] Public void Makepayment () {Var response 1 = "" & lt; XML & gt; .. & lt; / XML & gt; "; Var response 2 = @" & lt; XML & gt; .. & lt; / XML & gt; "; TestWebRequest request 1 = TestWebRequestCreate.CreateTestRequest (response1); TestWebRequest request 2 = TestWebRequestCreate.CreateTestRequest (response2); Var Results = webService.MakePayment (1, New PaymentRequest ()); Var required Rispons = new Payment Response () {status = 23}; assurance. half (expected went receipt, result);}   

which does not work.

thanks < / P>

Have you tested an example that you give as a work test? Hoog Because is required Rispons immediately within your test code and webService.MakePayment can not be the same object returned by the call. If you are connected to the article she was examining it that 2 strings are the same, 2 are not complex types do not use the object TestWebRequest in which you instantiate your test.

that said, you Are also doing the test, they can complete, either Control the to 2 calls for webRequest.MakePayment or using Mücke the callback feature, 2 or one of any two requests of this type include (request code): < / P>

  WebRequestMock.Setup (x = & gt; X.MakePayment (1, this. Any of these & lt; PaymentRequest & gt; ()). Returns (IASAIT; Payment Response & gt; ()) Callback ((payment-request request, payment response response) => VerifyResponse Private Voide VerifyResponse (PaymentRequest request, PaymentResponse response) {// Make some checks to determine whether it is the first call or second, 2 requests Based on something specially different - I need a more real code than you before I can clear it / eg / i.e. (pseudocode) if (request.SomeValue == First Request value) {response.SouldEqual (FirstReponse);} Else {res ponse.ShouldEqual (secondReponse);}} I say that this is not an unwanted code because I'm not sure that the returned response can be captured and the callback (Because it is return value) or not.   

No comments:

Post a Comment