Hello, I tried running my consumer test and I am g...
# pact-net
a
Hello, I tried running my consumer test and I am getting following error in snap: I am using rest sharp. Kindly help to resolve this issue.
m
That error is pretty self explanatory. Are you making a call to the endpoint shown? Note: you must hit the pact mock server, not the real thing
See the examples
a
I am making call to below LOCALHOST server: Which configuration am I missing
m
Ots your API client that needs to make the call to the mock server. What does that log file say?
a
Snap of log file is as:
Will debugging i am getting proper response from api server as well but mocker provider service verification is failed.
Mock server response is as below;
m
It’s really clear, your API client needs to send the request to the Pact mock server, and it’s not currently doing that in the test. Find the code in your test that sends the API call, and check that the call is not going to the real server, but the Pact provided mock service
a
Hello, Should I follow these steps mentioned in link: https://github.com/pact-foundation/pact-mock_service?
m
No, that is implementation detail you shouldn’t need to be aware of
Perhaps you share with us the code that tests your API client, that’s the bit that matters
it’s testing the API client, but is giving the URL of the mock service to the API client so that it sends the request to the mock service (automatically provided by Pact), not the real API
This is almost certainly where you’re going wrong
a
I will make changes, attaching my current project for reference.
m
Your API Client class doesn’t do anything with the
baseUri
given to it
Copy code
using ContracttestingPACT.Model;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using PactNetV3BrokerConnect.Model.PactV11;
using RestSharp;
using System.Diagnostics;
using <http://System.Net|System.Net>;
using Xunit;
namespace ContracttestingPACT.Common
{
    public class ApiClient
    {
        private string postURL = "<https://dummy.restapiexample.com/api>"; // <- this is always used
        private RestClient _client;


        public ApiClient(string baseUri = null)
        {
            _client = new RestClient(postURL); // <- baseUri is ignored
        }

        
        public UserResponse TestPOSTMethodWithJsonData()
        {
           
            var request = new RestRequest(postURL + "/v1/create", <http://Method.Post|Method.Post>); // <- the URL looks to be hard coded here to postURL anyway
            request.AddParameter("name", "test");
            request.AddParameter("salary", "123");
            request.AddParameter("age", "23");

            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content-Type", "application/json");
            var response = _client.ExecutePost(request); // <- this sends the request to "<https://dummy.restapiexample.com/api>"
           
            var content = response.Content.ToString();
            
            
         
            if (response.StatusCode.Equals(System.Net.HttpStatusCode.OK))
            {
                return !String.IsNullOrEmpty(content) ?
                  JsonConvert.DeserializeObject<UserResponse>(content)
                  : null;
            }
            throw new Exception();
        }
    } }
a
Done the changes, still I am getting error.
m
What error are you getting?
I can see the problem, but I want to see what errors you are getting to debug the situation
a
During debug , I am getting error in this line
m
there should be logs that show the problem
make sure the path in your API client matches the path you said it would send in the test
they don’t line up in the code you gave me
a
Log:
dummy api I am trying to test
m
That’s not what is defined in your pact test
Also, I want to see the log file that Pact produces. Please see the pact docs for your version on how to do that
(presumably it should be logged to a console)
I’m sorry, but I can’t help you write code in .NET (I don’t know .NET). But the error is really really clear - your API client is not sending the request to the Pact mock server in the way you have setup the test.
a
I have solved all issues and error now my coding is running and verification is also happening the only issue is with generation of contract pact file which is not happening, if u can look into the project and let me know with resolution, it would be helpful
Issue resolved, it 's working thanks for all the support @Matt (pactflow.io / pact-js / pact-go)
👍 1