2010-02-27 55 views
2

使用起訂量我剛開始版本(3.1)和我已閱讀博客和不....反正......我想什麼,直到你讓你的手髒,你不會學習:)使用起訂量 - 問題

還好這裏是我測試...

var newProduct = new Mock<IPartialPerson>(); 
    newProduct.SetupGet(p => p.FirstName).Returns("FirstName"); 
    newProduct.SetupGet(p => p.MiddleName).Returns("MiddleName"); 
    newProduct.SetupGet(p => p.LastName).Returns("LastName"); 
    newProduct.SetupGet(p => p.EmailAddress).Returns("[email protected]"); 
    newProduct.SetupGet(p => p.UserID).Returns("UserID"); 

    //mock Escort repository 
    var mockEscortRepository = new Mock<IEscortRepository>();  
    mockEscortRepository.Setup(p => p.LoadAllEscorts()) 
     .Returns(newProduct.Object); //error 

錯誤1的最佳重載的方法 匹配 「Moq.Language.IReturns> .Returns(System.Collections.Generic。列表)' 有s青梅參數無效


錯誤2參數 '1':不能從 轉換 'App.Model.Interface.IPartialPerson' 到 'System.Collections.Generic.List'


public interface IPartialPerson 
    { 
     string FirstName { get; } 
     string MiddleName { get; } 
     string LastName { get; } 
     string EmailAddress { get; } 
     string FullName { get; } 
     string UserID { get; } 

    } 

public interface IEscortRepository 
    { 
     List<PartialPerson> LoadAllEscorts(); 
     List<PartialPerson> SelectedEscorts(List<PartialPerson> selectedEscorts);  
    } 

我有我想TES兩種方法t「LoadAllaEscorts」和「SelectedEscorts」

我將如何做兩種方法的測試?

回答

3

我有我想測試 「LoadAllaEscorts」 和 「SelectedEscorts」

那些是接口上的方法兩種方法。您不要針對接口或針對模擬對象編寫測試。您可以針對具體的類編寫測試。

某處有一個實現IEscortRepository的EscortRepository。我假設碰到數據庫。針對這個寫集成測試。

在代碼中的其他地方,您可能有一個類(稱爲「Foo」),它具有注入IEscortRepository依賴項(例如通過構造函數參數)。當您想針對Foo類編寫測試時,您可以使用Moq創建一個模擬IEscortRepository,返回固定的測試數據並將該模擬對象傳遞到您的Foo實例中。

另一個問題是您的IEscortRepository方法正在返回(或作爲參數)List <PartialPerson>。這些應該是IList <IPartialPerson>(或IEnumerable <T>,ICollection <T>或ReadOnlyCollection <T>)。最重要的部分是收集項目應該是一個接口類型(IPartialPerson)。

+1大官,誰正確的代碼:

using System; 
using System.Collections.Generic; 
using Moq; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      var newProduct = new Mock<IPartialPerson>(); 

      newProduct.SetupGet(p => p.FirstName).Returns("FirstName"); 
      newProduct.SetupGet(p => p.MiddleName).Returns("MiddleName"); 
      newProduct.SetupGet(p => p.LastName).Returns("LastName"); 
      newProduct.SetupGet(p => p.EmailAddress).Returns("[email protected]"); 
      newProduct.SetupGet(p => p.UserID).Returns("UserID"); 

      var mockEscortRepository = new Mock<IEscortRepository>(); 

      mockEscortRepository 
       .Setup(p => p.LoadAllEscorts()) 
       .Returns(new List<IPartialPerson> {newProduct.Object}); 

      IEscortRepository repository = mockEscortRepository.Object; 

      IList<IPartialPerson> escorts = repository.LoadAllEscorts(); 

      foreach (IPartialPerson person in escorts) 
      { 
       Console.WriteLine(person.FirstName + " " + person.LastName); 
      } 

      Console.ReadLine(); 

      // Outputs "FirstName LastName" 
     } 
    } 

    public interface IPartialPerson 
    { 
     string FirstName { get; } 
     string MiddleName { get; } 
     string LastName { get; } 
     string EmailAddress { get; } 
     string FullName { get; } 
     string UserID { get; } 
    } 

    public interface IEscortRepository 
    { 
     IList<IPartialPerson> LoadAllEscorts(); 
     IList<IPartialPerson> SelectedEscorts(IList<IPartialPerson> selectedEscorts); 
    } 
} 

(上面的例子是一個單元測試,它只是表明,起訂量的作品。)

請注意,你不不必爲屬性使用SetupGet;安裝程序也適用。

5

試試這個:

mockEscortRepository 
.Setup(p => p.LoadAllEscorts()) 
.Returns(new List<IPartialPerson>() { newProduct.Object }); 

當你說:

.Returns(newProduct.Object) 

你問起訂量返回一個特定的對象。編譯器會發現你的方法返回一個列表,所以它不會編譯,除非你提供一個列表讓它返回。所以你需要創建一個包含你想返回的對象的列表,然後讓Moq返回。

根據您的意見,您可能也有興趣測試一個列表與多個項目。要做到這一點,請創建一個包含多個項目的列表,然後讓Mock返回。這裏是你可以創建一個以上的項目列表的一種方法:

List<PartialPerson> escorts = new List<PartialPerson>(); 
for (int i = 0; i < 10; i++) 
{ 

    var escort = new Mock<IPartialPerson>(); 
    escort.SetupGet(p => p.FirstName).Returns("FirstName" + i); 
    escort.SetupGet(p => p.MiddleName).Returns("MiddleName" + i); 
    escort.SetupGet(p => p.LastName).Returns("LastName" + i); 
    escort.SetupGet(p => p.EmailAddress).Returns(i + "[email protected]"); 
    escort.SetupGet(p => p.UserID).Returns("UserID" + i); 
    escorts.Add(escort.Object); 
} 

mockEscortRepository 
    .Setup(p => p.LoadAllEscorts()) 
    .Returns(escorts); 

祝你好運,繼續潘潘!

+0

magnifico: 有沒有一種方法可以測試它應該返回多少項?來自LoadAllaEscorts()? 最後,我將如何測試List SelectedEscorts(List selectedEscorts); 如果我的問題聽起來很愚蠢,請裸露在我身上,我剛剛開始學習...... 感謝您的回覆。 – 2010-02-27 16:10:03

+0

該模擬將返回您放入它的任何內容。因此,對模擬「應該」返回的測試是沒有意義的。你將不得不根據真實的存儲庫編寫你的測試來做到這一點。 – 2010-02-27 16:14:36

+0

ic你的意思。 所以上面會返回一個項目?因爲我放了一件物品? 如果我想測試更多然後我會怎麼做? – 2010-02-27 16:48:40

1

您的模擬設置爲返回單個項目,並且它應該根據存儲庫接口返回一個List。