2013-12-19 4 views
0

httpserver에 일부 값을 전달하려고합니다. 이것은 내 수업입니다.클래스 개체를 만드는 방법은 무엇입니까?

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 
    } 
} 

및 작동 중입니다. 이제 Send 메서드에 대한 Xunit 테스트 클래스를 작성하려고합니다. 그 때문에 CommandRestClient 클래스에 액세스하기 위해 객체를 만들어야합니다. 하지만 난 serverAddress 가치가 없었어요. 난 CommandRestClient 클래스에 대한 개체를 하드 코어에 의해 주소 값을 또는 serverAddress 전달하지 않고 만들고 싶습니다. 도와주세요 감사합니다.

+0

가 적절한 값까지 (단일 또는 공장에서) 무엇 이건 당신이 CommandRestClient 구현을 테스트하고 확인 (그것을 컴파일하지 않은 경우). * ICommandRestClient를 사용하는 것을 테스트하면 위조 할 수 있습니다 (전체적으로 serverAddress를 잊어 버릴 수도 있음). – user2864740

+0

제목에 내 게시물이 반영되어 있지 않습니다. – BDR

답변

1

일반적으로 CommandRestClient를 테스트하려면 CommandRestClientTest라는 xunit 클래스를 만듭니다. 해당 클래스의 ServerAddress 상수를 하드 코딩하여 serverAddress를 하드 코드하고 해당 xunit 클래스에서 작성한 모든 CommandRestClient 인스턴스로 전달할 수 있습니다.

실제로 통합 테스트 인 송출 메소드를 테스트하는 경우에 유의하십시오. 순수한 단위 테스트가 되려면 외부 상호 작용을 조롱하고 CommandRestClient에서만 비즈니스 로직을 테스트하십시오.

단위에서는 일반적으로 [설정]이라고 표시된 초기화에 넣지 만 xunit은 모든 메소드에서 객체를 만들 것을 권장합니다.

샘플 코드

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

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

     //test, assert etc 
    } 
} 
+0

이런 식으로 뭐야? 개인 읽기 전용 CommandRestClient _commandRestClient = new CommandRestClient ("http : // localhost : 8088 /"); – user3044294

관련 문제