最佳答案
我试图模拟一个名为 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>>
如果你能帮忙,我将不胜感激。