Question

i am try to pass some values to httpserver. this is my class

public interface ICommandRestClient
{
    IRestResult Send(IMessageEnvelope envelope);
}
public class CommandRestClient : ICommandRestClient
{
    private readonly string _serverAddress;

    /// <summary>
    /// Use to configure server address
    /// </summary>
    /// <param name="serverAddress">Configured serveraddress</param>
    public CommandRestClient(string serverAddress)
    {
        _serverAddress = serverAddress;
    }

    public IRestResult Send(IMessageEnvelope envelope)
    {
    //do 
    }
}

and its working. now i am try to write some Xunit test class for Send method. for that i need to create an Object to access CommandRestClient class. but i did not have serverAddress value. i want to create an object for CommandRestClient class by hardcore the address values or without passing serverAddress. please help me thank you.

Was it helpful?

Solution

Typically you will create a xunit class called CommandRestClientTest to test CommandRestClient. You can hardcode a ServerAddress constant in that class to hardcode the serverAddress and pass it to every CommandRestClient instance you create in that xunit class.

Bear in mind that if you are actually testing a send method to somewhere that is an integration test. To be a pure unit test you would mock external interaction and only test business logic in CommandRestClient

In Unit you will typically put this in the initialization marked as [Setup] but xunit encourages you to create the object in every method.

sample code(haven't compiled it)

public class CommandRestClientTest
{
    const string testServerAddress = "localhost:8080";

    [Fact]
    public void TestSomeMethod()
    {
        CommandRestClient commandRestClient = new CommandRestClient(testServerAddress);

        //test, assert etc
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top