2016-09-20 26 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 

} 

} 

編輯我的代碼,我只需要在第二個IF在我的方法來測試語句。如上所述,我需要對doctorDetails的價值進行編碼以便從服務中獲取數據,因爲爲了從服務中獲取數據,應該存在細節,但事實並非如此。否則,唯一的選擇就是嘲笑它,並在數據庫儲存的信息,並調用該服務,但我不想現在

+1

是否可以從班級外部訪問'_myService'?你可以嘲笑它,並將它注入到IsValid居住的類中嗎? –

+0

@GrantWinney:我是新來的測試,是的,我想,怎麼寫測試我的要求,請幫我看看代碼 – Learner

+0

@GrantWinney:我簡單的問題是,如何進行硬編碼bvalue doctordetails的ratehr不是從服務獲取它,然後只測試第二個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); 
    } 
}