2012-08-27 75 views
45

我試圖嘲弄一類,稱爲UserInputEntity,其中包含一個名爲ColumnNames特性:(它包含其他的屬性,我只是簡化了它的問題)起訂量,SetupGet,懲戒屬性

namespace CsvImporter.Entity 
{ 
    public interface IUserInputEntity 
    { 
     List<String> ColumnNames { get; set; } 
    } 

    public class UserInputEntity : IUserInputEntity 
    { 
     public UserInputEntity(List<String> columnNameInputs) 
     { 
      ColumnNames = columnNameInputs; 
     } 

     public List<String> ColumnNames { get; set; } 
    } 
} 

我有一個演示類:

namespace CsvImporter.UserInterface 
{ 
    public interface IMainPresenterHelper 
    { 
     //... 
    } 

    public class MainPresenterHelper:IMainPresenterHelper 
    { 
     //.... 
    } 

    public class MainPresenter 
    { 
     UserInputEntity inputs; 

     IFileDialog _dialog; 
     IMainForm _view; 
     IMainPresenterHelper _helper; 

     public MainPresenter(IMainForm view, IFileDialog dialog, IMainPresenterHelper helper) 
     { 
      _view = view; 
      _dialog = dialog; 
      _helper = helper; 
      view.ComposeCollectionOfControls += ComposeCollectionOfControls; 
      view.SelectCsvFilePath += SelectCsvFilePath; 
      view.SelectErrorLogFilePath += SelectErrorLogFilePath; 
      view.DataVerification += DataVerification; 
     } 


     public bool testMethod(IUserInputEntity input) 
     { 
      if (inputs.ColumnNames[0] == "testing") 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 
} 

我試過下面的測試,在那裏我嘲笑實體,試圖讓ColumnNames屬性返回初始化List<string>(),但它不工作:

[Test] 
    public void TestMethod_ReturnsTrue() 
    { 
     Mock<IMainForm> view = new Mock<IMainForm>(); 
     Mock<IFileDialog> dialog = new Mock<IFileDialog>(); 
     Mock<IMainPresenterHelper> helper = new Mock<IMainPresenterHelper>(); 

     MainPresenter presenter = new MainPresenter(view.Object, dialog.Object, helper.Object); 

     List<String> temp = new List<string>(); 
     temp.Add("testing"); 

     Mock<IUserInputEntity> input = new Mock<IUserInputEntity>(); 

    //Errors occur on the below line. 
     input.SetupGet(x => x.ColumnNames).Returns(temp[0]); 

     bool testing = presenter.testMethod(input.Object); 
     Assert.AreEqual(testing, true); 
    } 

我得到狀態的錯誤,有一些無效參數+參數1無法從字符串轉換爲

System.Func<System.Collection.Generic.List<string>> 

任何幫助,將不勝感激。

回答

93

ColumnNamesList<String>類型,這樣,當你設置你需要在Returns調用傳遞一個List<String>作爲參數(或FUNC它返回一個List<String>

但隨着這條線,你正在嘗試的屬性返回只是一個string

input.SetupGet(x => x.ColumnNames).Returns(temp[0]); 

這是造成異常。

更改它返回整個列表:

input.SetupGet(x => x.ColumnNames).Returns(temp); 
+0

看起來像我需要休息一下。非常感謝您的幫助! (+1 n將在7分鐘內接受你的答案) –

+8

SetupGet()是我正在尋找的。謝謝! – imnk