2015-01-08 38 views
1

我已經使用上了班的倉庫構造方法注入,我已經注意到了以下工作:依賴注入 - 將客戶對象造成測試失敗

public CreateInvoiceResult CreateInvoice(string Code, int qty, string Name) 
    { 

     if (string.IsNullOrEmpty(Code) || qty <= 0 || repository.GetByName(Name).ID <= 0) 
     { 
      return new CreateInvoiceResult(false); 
     } 

但是改變如下代碼(在加入「客戶卡斯特')導致測試失敗?

public CreateInvoiceResult CreateInvoice(string stockCode, int quantity, string customerName) 
{ 
    Customer cust = repository.GetByName(Name); 

    if (string.IsNullOrEmpty(Code) || qty <= 0 || cust.ID <= 0) 
    { 
     return new CreateInvoiceResult(false); 
    } 

試驗例:

請你能解釋一下爲什麼發生這種情況,我該如何改正?

編輯:更新了使用起訂量,使用正確的資料庫測試:

[TestClass] 
public class MockCustomerRepositoryDBTests 
{ 
    public MockCustomerRepositoryDBTests() 
    { 
     IList<Customer> customers = new List<Customer> 
     { 
      new Customer { ID = 1, Name = "Jim Smith", 
       Address = "14 Main Road"}, 
      new Customer { ID = 2, Name = "Alex Smith", 
       Address = "78 Avanue"}, 
      new Customer { ID = 3, Name = "Paul Brown", 
       Address = "1 Main Road"} 
     }; 

     // Mock the CustomerRepositoryDB Repository using Moq 
     Mock<ICustomerRepository> mockCustomerRepository = new Mock<ICustomerRepository>(); 

     // Return a customer by Name 
     mockCustomerRepository.Setup(mr => mr.GetByName(
      It.IsAny<string>())).Returns((string s) => customers.Where(
      x => x.Name == s).Single()); 

     // Complete the setup of the Mock Customer Repository 
     this.MockCustomerRepository = mockCustomerRepository.Object; 
    } 

    public readonly ICustomerRepository MockCustomerRepository; 

    [TestMethod] 
    public void stockCodeIsNullOrEmpty() 
    { 
     //Arrange 
     var x = new InvoiceController(MockCustomerRepository); 

     //Act 
     bool result = x.CreateInvoice("", 1, "test").Success; 

     //Assert 
     Assert.AreEqual(result, false); 
    } 

走走System.InvalidOperationException:序列不包含任何元素'

+0

你應該問一個關於你的moq的單獨問題。 –

+0

無需刪除所有原始問題:)。我會放回去。 –

回答

1

您的來電

Customer _Customer = repository.GetByName(customerName); 

是可能會失去一些例外。

如果你看看你的類,你只初始化你的庫對象在這個構造:

public PartInvoiceController(ICustomerRepository custRepo) 
{ 
    this.repository = custRepo; 
} 

但是您提供您在測試中調用默認的構造函數:

//Arrange 
var x = new PartInvoiceController(); 

所以你的資料庫永遠不會被初始化,所以你會得到一個空引用異常。

爲什麼它沒有失敗呢?

當它是if語句的一部分時,它並不重要,因爲它從未執行過,因爲string.IsNullOrEmpty(stockCode)是真的,其他條件是使用conditional-or operator (||)合併的,如果它能夠說明評估條件會短路即使所有條件都未被評估,條件也會變爲真或假。

由於這一點以及其他條件從未被評估過,所以if語句被輸入,所以存儲庫爲null是從未執行過的代碼。

要糾正它,您需要提供一個在測試中使用的存儲庫(存根或模擬),或者在默認構造函數中創建默認存儲庫,或還原爲原始代碼(只要您不需要'不關心在此測試中未初始化的存儲庫)。

改變你的測試是這樣的:

[TestMethod] 
public void stockCodeIsNullOrEmpty() 
{ 
    ICustomerRepository testRepository = //create a test repository here 
    //Arrange 
    var x = new PartInvoiceController(testRespository); 

    //Act 
    bool result = x.CreatePartInvoice("", 1, "test").Success; 

    //Assert 
    Assert.AreEqual(result, false); 
} 

編輯

真的是你應該問你的模擬問題的另一個問題,但基本上你正在尋找一個名爲「測試」用戶,但沒有的您的模擬存儲庫中的用戶具有該名稱

+0

感謝您的幫助 - 我認爲它與「System.NullReferenceException:對象引用未設置爲對象的實例」失敗。 - 我會如何糾正? –

+0

我已經提供了有關如何在答案結束時更正它的信息。您需要在測試中提供存儲庫,否則該類將使用哪個存儲庫來獲取用戶? –

+0

啊好的是有道理,謝謝你的幫助:) –

1
Customer _Customer = repository.GetByName(customerName); 

成員變量「存儲庫」在使用前未初始化。您應該使用類PartInvoiceController的其他構造函數。