2012-08-27 10 views
45

usiłuję mock klasy, o nazwie UserInputEntity, który zawiera właściwość o nazwie ColumnNames: (robi zawierać inne właściwości, właśnie uproszczone go za pytanie)Moq, SetupGet, szydząc właściwość

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; } 
    } 
} 

mam klasy prezentera:

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; 
      } 
     } 
    } 
} 

próbowałem poniższy test, gdzie Mock podmiot, postarać się ColumnNames własności zwrócić zainicjowany List<string>() ale to nie działa:

[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); 
    } 

Błędy dostaję stan, że istnieją pewne nieprawidłowe argumenty Argument + 1 nie może zostać zamienione z ciągiem do

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

Każda pomoc będzie mile widziane.

Odpowiedz

93

ColumnNames jest właściwością typu List<String> więc podczas konfigurowania trzeba zdać List<String> w zaproszeniu Returns jako argument (lub func, które zwracają List<String>)

Ale z tej linii, którą próbujesz zwrócić tylko jeden string

zmienić go na powrót całą listę:

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

Wygląda na to, muszę odpocząć. Bardzo dziękuję za pomoc! (+1 n akceptuje ur ans w 7min) –

+8

SetupGet() było tym, czego szukałem. Dzięki! – imnk