127

UserInputEntityという名前のプロパティを含む という名前のクラスをモックしようとしColumnNamesています : (他のプロパティが含まれています。質問のために簡略化しました)

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

どんな助けでも大歓迎です。

4

2 に答える 2

245

ColumnNamesは型のプロパティであるList<String>ため、設定するときに、引数(またはを返す関数)として呼び出しにaList<String>を渡す必要があります。ReturnsList<String>

しかし、この行を使用すると、string

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

これが例外の原因です。

リスト全体を返すように変更します。

input.SetupGet(x => x.ColumnNames).Returns(temp);
于 2012-08-27T12:17:49.547 に答える