2012-08-27 10 views
45

मैं एक वर्ग उपहास करने के लिए कोशिश कर रहा हूँ, UserInputEntity कहा जाता है, जो एक संपत्ति ColumnNames कहा जाता है मजाक: (यह अन्य गुण हो करता है, मैं सिर्फ यह प्रश्न के लिए आसान बना दिया है)Moq, 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; 
      } 
     } 
    } 
} 

मैं निम्नलिखित परीक्षण, जहाँ मैं इकाई नकली की कोशिश की है, एक प्रारंभ List<string>() वापस जाने के लिए ColumnNames संपत्ति प्राप्त करने की कोशिश, लेकिन यह काम नहीं कर रहा

[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

ColumnNames प्रकार List<String> इसलिए जब आप आप एक तर्क (या एक समारोह है जो एक List<String> वापसी)

लेकिन इस लाइन के साथ आप कोशिश कर रहे हैं के रूप में Returns कॉल में एक List<String> उत्तीर्ण करने की आवश्यकता की स्थापना कर रहे हैं की संपत्ति है सिर्फ एक string

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

जो अपवाद उत्पन्न कर रहा है वापस जाने के लिए।

बदलें यह पूरी सूची वापस करने के लिए: जैसे मैं एक ब्रेक की जरूरत है

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

लग रहा है। आपकी सहायता के लिए बहुत धन्यवाद! (+1 एन 7 मिनट में आपका उत्तर स्वीकार करेगा) –

+8

SetupGet() जो मैं ढूंढ रहा था। धन्यवाद! – imnk