如何MOQ对此有何看法? [英] How to Moq this view?

查看:180
本文介绍了如何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

 公共接口IVIEW {
无效CloseView();
无效的setTitle(串题);
无效ShowView();
无效RaiseVoidEvent(VoidEventHandler @event);

事件VoidEventHandler OnViewInitialize;
事件VoidEventHandler OnViewShown;
}



IApplicationView



  public接口IApplicationView:IVIEW {
无效OnUserAuthenticated();

事件VoidEventHandler ManageRequestsClicked;
}



IPresenter



 公共接口IPresenter< V>其中V:IVIEW {
v查看{搞定; }
IDatabaseUser的currentUser {搞定; }

无效CloseView();
无效OnViewInitialize();
无效RaiseVoidEvent(VoidEventHandler @event);
无效ShowView();

事件VoidEventHandler OnCloseView;
事件VoidEventHandler OnShowView;
}



主持人



 公共抽象类主持人< V> :IPresenter< V>其中V:IVIEW {
公开演示(V视图){
如果(查看== NULL)抛出新的ArgumentNullException(意见);

=查看视图;
View.OnViewInitialize + = OnViewInitialize;

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

公共虚拟IDatabaseUser的currentUser {搞定;保护套; }
公共虚拟v查看{搞定;私人集; }

公共虚拟无效CloseView(){RaiseVoidEvent(OnCloseView); }
公共虚拟无效OnViewInitialize(){}
公共无效RaiseVoidEvent(VoidEventHandler @event){如果(@event!= NULL)@event(); }
公共虚拟无效ShowView(){RaiseVoidEvent(OnShowView); }

公共虚拟活动VoidEventHandler OnCloseView;
公共虚拟活动VoidEventHandler OnShowView;
}



IApplicationPresenter



  public接口IApplicationPresenter:IPresenter< IApplicationView> {
IAuthenticationPresenter AuthenticationPresenter {搞定;组; }

无效OnManageRequestsClicked();
无效OnUserAuthenticated(UserAuthenticatedEventArgs E);
无效OnViewShown();
}



ApplicationPresenter



 公共类ApplicationPresenter:主持人< IApplicationView>中IApplicationPresenter {
公共ApplicationPresenter(IApplicationView视图):这个(查看,NULL){}
公共ApplicationPresenter(IApplicationView观点,IAuthenticationPresenter authPresenter):基地(视图){
AuthenticationPresenter = authPresenter;
View.OnViewShown + = OnViewShown;
View.ManageRequestsClicked + = OnManageRequestsClicked;
}

公共IAuthenticationPresenter AuthenticationPresenter {{返回authenticationPresenter; }集合{setAuthenticationPresenter(值); }}

公共无效OnManageRequestsClicked(){
变种请求=新GestionDemandeAccesInformationForm();
requests.Database =数据库;
requests.MdiParent =(System.Windows.Forms.Form中)查看;
requests.Show();
}

公共无效OnUserAuthenticated(UserAuthenticatedEventArgs E){
的currentUser =新DatabaseUser(e.Login,e.Password,e.DatabaseInstance);
数据库=新DatabaseSessionFactory(的currentUser);
setAppTitle();
showRequestsManagementView();
}

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



IAuthenticationView



  public接口IAuthenticationView:IVIEW {
串的ErrorMessage {搞定;组; }
字符串实例{搞定;组; }
IEnumerable的<串GT;实例{搞定;组; }
串登录{搞定;组; }
串密码{搞定;组; }

无效EnableConnectButton(布尔启用);
无效SetDefaultInstance(字符串defaultInstance);
无效RaiseSelectionChangedEvent(SelectionChangedEventHandler @event,SelectionChangedEventArgs E);

事件VoidEventHandler的onConnect;
事件SelectionChangedEventHandler OnDatabaseInstanceChanged;
事件VoidEventHandler OnLoginChanged;
事件VoidEventHandler OnPasswordChanged;
}



IAuthenticationPresenter



  public接口IAuthenticationPresenter:IValidatablePresenter,IPresenter< IAuthenticationView> {
无效的onConnect();
无效OnViewDatabaseInstanceChanged(SelectionChangedEventArgs E);
无效OnViewLoginChanged();
无效OnViewPasswordChanged();
无效RaiseUserAuthenticatedEvent(UserAuthenticatedEventArgs E);
事件UserAuthenticatedEventHandler UserAuthenticated;
}



AuthenticationPresenter



 公共类AuthenticationPresenter:主持人< IAuthenticationView>中IAuthenticationPresenter {
公共AuthenticationPresenter(IAuthenticationView观点,IMembershipService服务):基地(视图){
MembershipService =服务;
View.ErrorMessage = NULL;
View.SetTitle(ViewTitle);
subscribeToEvents();
}

公共BOOL的IsValid {{返回credentialsEntered(); }}
公共IMembershipService MembershipService {搞定;组; }

公共虚拟无效的onConnect(){
如果(noDatabaseInstanceSelected())显示(MissingInstanceErrorMessage);
,否则如果(noLoginEntered())显示(MissingLoginErrorMessage);
,否则如果(noPasswordEntered())显示(MissingPasswordErrorMessage);
,否则{
显示器(EverythingIsFine);
如果(isAuthenticUser())notifyTheApplicationThatTheUserIsAuthentic();
,否则{显示(InvalidLoginOrPasswordErrorMessage); }
}
}

公共覆盖无效OnViewInitialize(){
base.OnViewInitialize();
View.ErrorMessage = NULL;
View.Instances = Configuration.DatabaseInstances;
View.SetDefaultInstance(Configuration.DefaultInstance);
}

公共无效OnViewDatabaseInstanceChanged(SelectionChangedEventArgs E){View.Instance =(字符串)e.Selected; }
公共无效OnViewLoginChanged(){View.EnableConnectButton(IsValid的); }
公共无效OnViewPasswordChanged(){View.EnableConnectButton(IsValid的); }
公共无效RaiseUserAuthenticatedEvent(UserAuthenticatedEventArgs E){如果(UserAuthenticated!= NULL)UserAuthenticated(E); }

公共事件UserAuthenticatedEventHandler UserAuthenticated;
}


解决方案

如果我是你,我倒是注入一个工厂,用于创建 AuthenticationPresenter 并在测试我称之为 OnViewShown()和您的模拟验证(那 ShowView 由工厂返回演示)被调用。



修改
注意,我没有编这个,我没有一个C#编译器现在。



下面是我的版本的测试。根据我的理解你真正想要测试一下:



  [TestClass中] 
公共类ApplicationPresenterTests
{
[TestClass中]
公共类OnViewShown:ApplicationPresenterTests
{
[TestMethod的]
公共无效ForceAuthentication()
{
//给出
变种authenticationPresenterFactory =新的模拟< IAuthenticationPresenterFactory>();
变种authenticationPresenter =新的模拟< IAuthenticationPresenter>();
authenticationPresenterFactory.Setup(F => f.create())。返回(authenticationPresenter.Object);
变种主持人=新ApplicationPresenter(authenticationPresenterFactory);

//当
presenter.OnViewShown();

//然后
authenticationPresenter.Verify(P => p.ShowView());
}
}


I have a view for which I'd like to mock the Show behaviour.

Once the credentials have been entered, the [Connecter] button enables itself, and then the user can click. I wish I could reproduce this behaviour without having to show the view and actually really enter my credentials.

The application is a WinForms MDI presented by the IApplicationPresenter. The IApplicationPresenter raises the ShowView to which the IApplicationView subscribed.

Then, when the IApplicationView.Shown, the IApplicationPresenter forces the user to authenticate like this:

IApplicationPresenter.OnViewShown

public void OnViewShown() { forceAuthentication(); }

private void forceAuthentication() {
    IAuthenticationView authView = new AuthenticationView();
    IAuthenticationPrenseter authPresenter = new AuthenticationPresenter();
    authPresenter.ShowView();
}

It's like I can smell one thing.

  1. It's just like I could inject the IAuthenticationView to the IApplicationPresenter. Then, this would allow me to inject my mocked view to it, and avoid the view being actually shown, which is in fact what I want to come up with. Is it the best way to make it?

Now, I want to test that when the IApplicationView is shown, the IApplicationPresenter is notified and forces authentication.

Any thoughts of a better approach in terms of mocking here?

UPDATE

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.

EDIT Note 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对此有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆