本文介绍了如何MOQ对此有何看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想为其嘲弄显示行为的情况。





一旦凭证已被输入,[连接器]按钮使本身,然后用户可以点击。我希望我能重现此行为,而不必显示视图,实际上真正进入我的凭据。



该应用程序是通过提出一个WinForms MDI的 IApplicationPresenter 。在 IApplicationPresenter 提高了 ShowView 来该 IApplicationView 认购。



然后,当 IApplicationView.Shown IApplicationPresenter 强制用户这样的验证:



IApplicationPresenter.OnViewShown

 公共无效OnViewShown(){forceAuthentication(); } 

私人无效forceAuthentication(){
IAuthenticationView authView =新AuthenticationView();
IAuthenticationPrenseter authPresenter =新AuthenticationPresenter();
authPresenter.ShowView();
}



这就像我可以闻到一件事。




  1. 这就像我可以注入 IAuthenticationView IApplicationPresenter 。然后,这将让我注入我嘲笑认为它,避免被认为实际显示,这实际上是什么,我想拿出。它是使它的最好方法是什么?



现在,我想测试,当 IApplicationView 所示, IApplicationPresenter 通知并强制认证。



一个更好的办法的任何想法在嘲讽这里条件怎么样?



IView

public interface IView {
    void CloseView();
    void SetTitle(string title);
    void ShowView();
    void RaiseVoidEvent(VoidEventHandler @event);

    event VoidEventHandler OnViewInitialize;
    event VoidEventHandler OnViewShown;
}

IApplicationView

public interface IApplicationView : IView {
    void OnUserAuthenticated();

    event VoidEventHandler ManageRequestsClicked;
}

IPresenter

public interface IPresenter<V> where V : IView {
    V View { get; }
    IDatabaseUser CurrentUser { get; }

    void CloseView();
    void OnViewInitialize();
    void RaiseVoidEvent(VoidEventHandler @event);
    void ShowView();

    event VoidEventHandler OnCloseView;
    event VoidEventHandler OnShowView;
}

Presenter

public abstract class Presenter<V> : IPresenter<V> where V : IView {
    public Presenter(V view) {
        if (view == null) throw new ArgumentNullException("view");

        View = view;
        View.OnViewInitialize += OnViewInitialize;

        OnCloseView += View.CloseView;            
        OnShowView += View.ShowView;
    }

    public virtual IDatabaseUser CurrentUser { get; protected set; }
    public virtual V View { get; private set; }

    public virtual void CloseView() { RaiseVoidEvent(OnCloseView); }
    public virtual void OnViewInitialize() { }
    public void RaiseVoidEvent(VoidEventHandler @event) { if (@event != null) @event(); }
    public virtual void ShowView() { RaiseVoidEvent(OnShowView); }

    public virtual event VoidEventHandler OnCloseView;
    public virtual event VoidEventHandler OnShowView;
}

IApplicationPresenter

public interface IApplicationPresenter : IPresenter<IApplicationView> {
    IAuthenticationPresenter AuthenticationPresenter { get; set; }

    void OnManageRequestsClicked();
    void OnUserAuthenticated(UserAuthenticatedEventArgs e);
    void OnViewShown();
}

ApplicationPresenter

public class ApplicationPresenter : Presenter<IApplicationView>, IApplicationPresenter {
    public ApplicationPresenter(IApplicationView view) : this(view, null) { }
    public ApplicationPresenter(IApplicationView view, IAuthenticationPresenter authPresenter) : base(view) {
        AuthenticationPresenter = authPresenter;            
        View.OnViewShown += OnViewShown;
        View.ManageRequestsClicked += OnManageRequestsClicked;
    }

    public IAuthenticationPresenter AuthenticationPresenter { get { return authenticationPresenter; } set { setAuthenticationPresenter(value); } }

    public void OnManageRequestsClicked() {
        var requests = new GestionDemandeAccesInformationForm();
        requests.Database = database;
        requests.MdiParent = (System.Windows.Forms.Form)View;
        requests.Show();
    }

    public void OnUserAuthenticated(UserAuthenticatedEventArgs e) { 
        CurrentUser = new DatabaseUser(e.Login, e.Password, e.DatabaseInstance);
        database = new DatabaseSessionFactory(CurrentUser);
        setAppTitle();
        showRequestsManagementView();
    }

    public void OnViewShown() { forceAuthentication(); }
}

IAuthenticationView

public interface IAuthenticationView : IView {
    string ErrorMessage { get; set; }
    string Instance { get; set; }
    IEnumerable<string> Instances { get; set; }
    string Login { get; set; }
    string Password { get; set; }

    void EnableConnectButton(bool enabled);
    void SetDefaultInstance(string defaultInstance);
    void RaiseSelectionChangedEvent(SelectionChangedEventHandler @event, SelectionChangedEventArgs e);

    event VoidEventHandler OnConnect;
    event SelectionChangedEventHandler OnDatabaseInstanceChanged;
    event VoidEventHandler OnLoginChanged;
    event VoidEventHandler OnPasswordChanged;
}

IAuthenticationPresenter

public interface IAuthenticationPresenter : IValidatablePresenter, IPresenter<IAuthenticationView> {
    void OnConnect();
    void OnViewDatabaseInstanceChanged(SelectionChangedEventArgs e);
    void OnViewLoginChanged();
    void OnViewPasswordChanged();
    void RaiseUserAuthenticatedEvent(UserAuthenticatedEventArgs e);
    event UserAuthenticatedEventHandler UserAuthenticated;
}

AuthenticationPresenter

public class AuthenticationPresenter : Presenter<IAuthenticationView>, IAuthenticationPresenter {
    public AuthenticationPresenter(IAuthenticationView view, IMembershipService service) : base(view) {
        MembershipService = service;
        View.ErrorMessage = null;
        View.SetTitle(ViewTitle);
        subscribeToEvents();
    }

    public bool IsValid { get { return credentialsEntered(); } }
    public IMembershipService MembershipService { get; set; }

    public virtual void OnConnect() {
        if (noDatabaseInstanceSelected()) display(MissingInstanceErrorMessage);
        else if (noLoginEntered()) display(MissingLoginErrorMessage);
        else if (noPasswordEntered()) display(MissingPasswordErrorMessage);
        else {
            display(EverythingIsFine);
            if (isAuthenticUser()) notifyTheApplicationThatTheUserIsAuthentic();
            else { display(InvalidLoginOrPasswordErrorMessage); }
        }
    }

    public override void OnViewInitialize() {
        base.OnViewInitialize();
        View.ErrorMessage = null;
        View.Instances = Configuration.DatabaseInstances;
        View.SetDefaultInstance(Configuration.DefaultInstance);
    }

    public void OnViewDatabaseInstanceChanged(SelectionChangedEventArgs e) { View.Instance = (string)e.Selected; }
    public void OnViewLoginChanged() { View.EnableConnectButton(IsValid); }
    public void OnViewPasswordChanged() { View.EnableConnectButton(IsValid); }
    public void RaiseUserAuthenticatedEvent(UserAuthenticatedEventArgs e) { if (UserAuthenticated != null) UserAuthenticated(e); }

    public event UserAuthenticatedEventHandler UserAuthenticated;
}
解决方案

If I were you, I'd inject a factory for creating AuthenticationPresenter and in your test I would call OnViewShown() and verify on your mock (of the presenter returned by the factory) that ShowView is called.

EDITNote that I haven't compiled this, I don't have a C# compiler right now.

Here is my version of the test. Based on my interpretation of what you really want to test :

[TestClass]
public class ApplicationPresenterTests 
{
    [TestClass]
    public class OnViewShown : ApplicationPresenterTests 
    {
        [TestMethod]
        public void ForceAuthentication() 
        {
            // given
            var authenticationPresenterFactory = new Mock<IAuthenticationPresenterFactory>();
            var authenticationPresenter = new Mock<IAuthenticationPresenter>();
            authenticationPresenterFactory.Setup(f => f.create()).Returns(authenticationPresenter.Object);
            var presenter = new ApplicationPresenter(authenticationPresenterFactory);

            // when
            presenter.OnViewShown();

            // then
            authenticationPresenter.Verify(p => p.ShowView());
        }
}

这篇关于如何MOQ对此有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 13:00