【问题标题】:How to unit test an event in MVP using MOQ and the repository pattern?如何使用 MOQ 和存储库模式对 MVP 中的事件进行单元测试?
【发布时间】:2017-10-28 14:55:54
【问题描述】:

我环顾四周,找不到任何可以帮助我的地方。

所以我想单元测试的事件是我的 OnFormLoadEvent。它看起来像这样:

public partial class SystemVariablesForm : Form, ISystemVariablesView {

        private SystemVariablesPresenter presenter;
        private readonly ISystemVariablesManager manager;

        public SystemVariablesForm(ISystemVariablesManager _manager) {
            manager = _manager;
            InitializeComponent();
        }

        public float BindingLip {
            get {
                return (float)nudBindingLip.Value;
            }
            set => nudBindingLip.Value = (decimal)value;
        }
        public float HeadTrim {
            get {
                return (float)nudHeadTrim.Value;
            }
            set => nudHeadTrim.Value = (decimal)value;
        }
        public float FootTrim {
            get {
                return (float)nudFootTrim.Value;
            }
            set => nudFootTrim.Value = (decimal)value;
        }

        public string ErrorMessage {
            get {
                return lblErrors.Text;
            }
            set => lblErrors.Text = value;
        }
        public event EventHandler<EventArgs> SetSystemVariables;

        public event EventHandler<EventArgs> OnFormLoad;
        public event EventHandler<ErrorEventArgs> LogErrorToView;
        public event EventHandler<EventArgs> SetImpositionFormAsActive;

        private void SetSystemVariables_Load(object sender, EventArgs e) {
            //Have to do this to avoid a dependency injection loop as the view relies on the presenter and the presenter relies on the view
            presenter = new SystemVariablesPresenter(this, manager);
            try {
                OnFormLoad(this, e);
            }
            catch (Exception ex) {
                LogErrorToView(this, new ErrorEventArgs(ex.Message));
            }
        }
    }

然后通过这种方法在我的演示者中获取:

private void DisplaySystemVariables(object sender, EventArgs e) {
    try {
        SystemVariables variables = _systemVariablesManager.ReturnSystemVariables();
        _view.BindingLip = variables.BindingLip;
        _view.HeadTrim = variables.HeadTrim;
        _view.FootTrim = variables.FootTrim;
    }
    catch (Exception ex) {
        LogErrorToView(this, new ErrorEventArgs(ex.Message));
    }
}

这叫我的经理:

  public class SystemVariablesManager : ISystemVariablesManager {
        private ISystemVariablesRepository _systemVariablesRepo;
        public SystemVariablesManager(ISystemVariablesRepository systemVariablesRepo) {
            _systemVariablesRepo = systemVariablesRepo;
        }    
        public Models.SystemVariables ReturnSystemVariables() {
            return _systemVariablesRepo.ReturnSystemVariables();
        }

        public void SetSystemVariables(Models.SystemVariables systemVariables) {
            _systemVariablesRepo.SetSystemVariables(systemVariables);
        }
    }

依次调用我的存储库:

   public Models.SystemVariables ReturnSystemVariables() {
            if (File.Exists(expectedFilePath)) {
                var json = JObject.Parse(File.ReadAllText(expectedFilePath))["SystemVariables"];
                return JsonConvert.DeserializeObject<Models.SystemVariables>(json.ToString());
            }
            else {
                throw new Exception("Setup file not located. Please run the Inital Set up application. Please ask Andrew for more information.");
            }
        }

现在我需要使用单元测试来测试这个事件,我选择了 MOQ,但我不确定如何使用它来测试它。

到目前为止,我的单元测试如下所示:

  [TestClass]
    public class SystemVariablesPresenterTests {

        [TestMethod]
        private void OnFomLoad() {
            var mockView = new Mock<ISystemVariablesView>();

            mockView.Raise(r => r.OnFormLoad += null, new EventArgs());

            Assert.IsNotNull(mockView.Object.HeadTrim);

        }
    }

如何修改我的单元测试以像上述步骤一样调用存储库/管理器?

抱歉,对这个很陌生。

【问题讨论】:

    标签: c# unit-testing repository-pattern mvp


    【解决方案1】:

    模拟视图只是您的模拟。您还需要创建测试目标,它应该是演示者。我想有一种方法可以将模拟视图连接到真正的演示者。您还需要确定您的单元的边界,通常只有演示者。然后,您将需要模拟外部依赖项。由于边界只是演示者,因此管理器是需要模拟的外部依赖项。基本上,您只是在测试,当您的模拟视图触发一个事件时,您的测试目标(真正的演示者)正在对模拟经理做什么。存储库不参与此过程。

    您也可以采取务实的态度,确定单位是主持人和经理结合在一起。然后存储库成为外部依赖项,需要模拟。然后,您将测试模拟视图何时触发和事件,测试目标(真正的演示者 + 经理)正在对模拟存储库执行什么操作。

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多