2016-09-20 1 views
0

테스트중인 큰 메서드 내부에서 몇 가지 메서드와 조건을 빠져 나가거나 무시하는 테스트를 작성하는 방법입니다. 문이 내 방식으로 테스트 할 경우 메서드 내부의 일부 메서드와 테스트중인 메서드의 일부 조건을 생략하여 단위 테스트 작성

public bool IsValid(int id) 
{ 

var details = _myService.GetDetails(id); // This line should be avoided in test 
var doctorDetails = _myService.GetDoctorDetails("AUS"); // This needs to be executed 

if(details.Name == "Ab") // This if I dont want to be part of my test 
{ 
// Do something 

} 

if(doctorDetails !=null) 
{ 
// Code to test 

} 

} 

내 코드를 편집

, 나는 단지 두 번째가 필요합니다

예를 들어, 나는 아래에이 방법을 가지고있다. 위에 표시된 것처럼 서비스에서 데이터를 가져 오는 것에 대해 doctorDetails의 가치를 해독해야합니다. 왜냐하면 서비스에서 가져 오기 위해 세부 정보가 있어야하기 때문입니다. 그러나 그렇지 않습니다. 그렇지 않은 경우 모의하고 DB에 세부 정보를 저장하고이 서비스를 호출하면됩니다. 지금은 원하지 않습니다.

+1

클래스 외부에서 '_myService'에 액세스 할 수 있습니까? 당신은 그것을 조롱하고,'IsValid'가 살고있는 수업에 그것을 주입시킬 수 있습니까? –

+0

@GrantWinney : 나는 테스트에 초보입니다. 제 요구 사항에 대한 테스트 작성 방법은 코드 – Learner

+0

으로 도와주세요. @GrantWinney : 제 간단한 질문은 doctordetails의 bvalue를 서비스에서 가져 오는 것보다 두 번째 if 문만 테스트하십시오. Doctordetails는 문자열 값입니다. – Learner

답변

1

개념은 우리가 다른 서비스를 스태핑 할 수 있어야만 두 번째 IF 문의 실행. 우리는 단위 테스트의 목적에 어긋나므로 실제 서비스와 대화하고 싶지 않습니다. 미안하지만 당신이 단위 테스트에 의도 한 정확한 동작을 확신 할 수는 없지만 아래에서 바라는 바는 올바른 방향으로 향할 것입니다.

public class PatiantDetail { 
    public string Name { get; set; } 
} 

public class DoctorDetail { 
    public string Name { get; set; } 
} 

public interface IService { 
    PatiantDetail GetPatiantDetail(int id); 
    DoctorDetail GetDoctorDetail(string countryCode); 
} 

//System Under Test 
public class Sut 
{ 
    private readonly IService _myService; 

    public Sut(IService service) 
    { 
     _myService = service; 
    } 

    public bool IsValid(int id) 
    { 
     var details = _myService.GetPatiantDetail(id); // This line should be avoided in test 
     var doctorDetails = _myService.GetDoctorDetail("AUS"); // This needs to be executed 

     if (details.Name == "Ab") // This if I don't want to be part of my test 
     { // Do something 
     } 

     if (doctorDetails != null) 
     {// Code to test 
     } 

     return true; 
    } 
} 


[TestClass] 
public class UnitTestClass 
{ 
    [TestMethod] 
    public void TestMethodForDemostationOfStubbing() 
    { 
     //Qustion: "How to write tests that escapes or bypasses few methods and conditions inside a big method that is being tested" 
     //Answer: 
     //This is pivotal to Unit Testing as we should be able to isolate dependencies and test what is only reequired. 
     //As soon as we concern about calling real services/db, it becomes an Integration Test :) 
     //There are many ways to do isolate dependencies in your test. For example, 
     // a. Poor man's techniques. (too much code you may have to write) 
     // b. Using an isolation framework. i.e Rhyno Mock you already using. (less code you have to write) 


     //Using your example 
     //"I need only the second IF statement to be tested in my method." 

     //Arrange 
     int id = 3; 
     var stubService = MockRepository.GenerateStub<IService>(); 
     stubService.Expect(x => x.GetPatiantDetail(Arg<int>.Is.Anything)).Return(new PatiantDetail() {Name = ""}); //so the first *if* statement get bypass. i.e not to return "Ab" 

     //"I need to harcode the value of doctorDetails against to fetching its data from service as indicated above" 
     stubService.Expect(x => x.GetDoctorDetail(Arg<string>.Is.Anything)).Return(new DoctorDetail()); //returns an object with any hard coded data so the second *if* statement get executed. 


     var sut = new Sut(stubService); 

     //Act 
     var isValid = sut.IsValid(id); 

     //Assert 
     Assert.IsTrue(isValid); 
    } 
} 
관련 문제