简体   繁体   English

如何使用MOQ和存储库模式对MVP中的事件进行单元测试?

[英]How to unit test an event in MVP using MOQ and the repository pattern?

I've had a look around and can't find anywhere to help me. 我环顾四周,找不到任何可以帮助我的地方。

So the event on my view which I would like to unit test is my OnFormLoadEvent. 所以我希望单元测试的观点是我的OnFormLoadEvent。 It looks like this: 它看起来像这样:

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

This then gets picked up in my presenter in this method: 然后在我的演示者中使用此方法获取:

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

This calls my manager: 这叫我的经理:

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

Which in turn calls my repository: 反过来调用我的存储库:

   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.");
            }
        }

Now I need to test this event using unit tests, I have chosen MOQ but I'm unsure how I can use it to test this. 现在我需要使用单元测试来测试这个事件,我选择了MOQ,但我不确定如何使用它来测试它。

My unit test looks like this so far: 到目前为止我的单元测试看起来像这样:

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

        }
    }

How do I modify my unit test to call the repository/manager like the above steps? 如何像上述步骤一样修改单元测试以调用存储库/管理器?

Sorry, very new to this. 对不起,这个很新。

The mocked view is only your mock. 模拟的视图只是你的模拟。 You will also need to create the test target, which should be the presenter. 您还需要创建测试目标,该目标应该是演示者。 I suppose there is a way to connect the mock view to the real presenter. 我想有一种方法可以将模拟视图连接到真正的演示者。 You will also need to decide the boundary of your unit which is usually only the presenter. 您还需要确定单位的边界,通常只是演示者。 Then you will need to mock the external dependencies. 然后,您将需要模拟外部依赖项。 Since the boundary is only the presenter, the manager is an external dependency which needs to be mocked. 由于边界只是演示者,因此管理者是需要模拟的外部依赖。 Basically you are only testing, when your mocked view fires an event, what your test target(the real presenter) is doing to the mocked manager. 基本上,您只是在测试时,当您的模拟视图触发事件时,您的测试目标(真正的演示者)正在对模拟的管理器做什么。 The repository is not involved in this process. 存储库不参与此过程。

You can also take an pragmatic approach, and decide that the unit is the presenter and the manager combined together. 您还可以采取务实的方法,并确定单位是演示者和经理组合在一起。 Then the repository becomes an external dependency and needs to be mocked. 然后,存储库成为外部依赖项,需要进行模拟。 Then you will be testing when the mocked view fires and event, what the test target(the real presenter + manager) is doing to the mocked repository. 然后,您将测试模拟视图何时触发和事件,测试目标(真实演示者+管理者)对模拟存储库执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM