- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在我的应用程序中使用 MVP 模式。但是我在测试单击按钮后调用的方法时遇到问题。这是代码:
public interface IControl
{
bool Enabled { get; set; }
string Text { get; set; }
}
public interface IButton : IControl
{
event EventHandler Click;
}
public class Button : System.Windows.Forms.Button, IButton
{ }
public interface IForm : IControl
{
void Show();
void Close();
}
public interface IView : IForm
{
IButton Button1 { get; }
}
public partial class View : Form, IView
{
public View()
{
InitializeComponent();
}
#region IView Members
public IButton Button1
{
get { return button1; }
}
#endregion
}
public class Presenter
{
IView view;
public Presenter(IView view)
{
this.view = view;
this.view.Button1.Click += ButtonClick;
this.view.Show();
}
private void ButtonClick(object sender, EventArgs e)
{
view.Button1.Text= "some text";
}
}
问题是我不知道如何编写测试以便调用我的 ButtonClick 方法。我试过这样:
var view = new Mock<IView>();
view.Setup(x => x.Button1).Returns(new Mock<IButton>().SetupAllProperties().Object);
Presenter presenter = new Presenter(view.Object);
view.Raise(x => x.Button1.Click+= null, EventArgs.Empty);
Assert.AreEqual("some text", view.Object.Button1.Text);
我认为问题出在这一行:
this.view.Button1.Click += ButtonClick;
Click 事件似乎不记得 ButtonClick 方法。如何让Click to be stub正常工作。欢迎任何建议。提前致谢。问候,瓦依达
编辑:我在创建 SubscribeOnClick(EventHandler click) 时能够做到这一点;我的 IButton 界面中的方法,而不是事件 EventHandler Click。我在记得方法的地方制作了一些 ButtonMock。但是,如果有人知道更好的解决方案,请与我分享。
最佳答案
也许使用 command pattern 不是一个坏主意这里。你的IView
是非常特定于实现的,因为它有规定数量的控件,这些控件应该有一个 Click
事件(我知道这是一个例子,但仍然......)。
命令模式的一个简单实现是让 IView
有一个List<Action>
由演示者提供,并让 View 的特定实现决定如何触发这些操作,例如通过做
this.button1.Click += (sender, e) => this.Actions[0]();
模拟对象不需要有 Click 事件(Moq 甚至可能不支持,我不确定)。您可以让它触发其中一个 Action 。
关于c# - 使用最小起订量测试 WinForms MVP 和事件机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7570343/
我相信 Presenter 是负责处理 MVP 应用程序导航方面的人。这是真的还是有异常(exception)? 最佳答案 我认为 View 是唯一负责屏幕导航的 View 。 Here我们对这个“A
我看到了两种实现“加载”反馈的基本方法(例如旋转加载图标): 在演示者中: void displayData() { display.startShowingLoadingIcon();
我有一个方法,用户可以搜索文章编号,如果它在数据库中可用,则文章编号绑定(bind)到 BindingList。现在我想让用户知道该文章是否在数据库中不可用。我该如何以正确的方式做到这一点? 只需将消
我们正在使用 gwt-presenter,但并不是一个特定于此的问题...... 我有一张包含用户的表格。当我在 View 中构建表格时(根据演示者提供的数据),我需要在行的末尾添加两个操作按钮(“编
请用一个简单的例子来描述MVP-Passive View和MVP-Supervising controller的区别。最好显示如何使用 mvp 技术(被动 View 和监督 Controller )绑
GWT 2.1 包含一个 MVP framework其中包括 Google 的 Ray Ryan 在 Google I/O 2009 上概述的 PlaceService 实现。 那个talk启发了一些
在Model-View-Presenter模式中,我们应该在哪里编写用户输入的验证。 最佳答案 Realm 特定的规则/验证应在模型中。您可以使用model.validate()来告知您是否违反规则。
当我们在面试的时候被问到谈谈你对MVC,MVP,MVVM的理解。我们该如何回答,下面我们将对这些模式进行总结。 一、web1.0时代 在web1.0时代,当时并没有前端这个概念,当时如果要开发一个项目
我正在寻找有关 GWT 架构的指南 - 何时使用自包含小部件与 MVP/事件/场所。 背景 阅读了 Google 文档并搜索了 Stackoverflow,gwt-examples 项目为这个问题提供
在地点更改时更新事件状态的最佳做法是什么?假设您有一个带有 View 的事件,该 View 显示类别列表和类别中的项目列表。如果选择了不同的类别,则应用程序会使用类别 ID 转到新位置。然后我只想刷新
在 GWT 中使用 MVP 时,您将如何使用表?例如,如果您有一个用户表,您的 View 看起来像这样吗? public interface MyDisplay{ HasValue users();
我有一个 GWT 应用程序,我正在将 GWT MVP 与地点/事件一起使用。 我的应用程序布局类似于 菜单 |内容 菜单和内容显示将动态变化,其中一个单独变化。我的意思是,当内容显示更改时,我不想更新
我正在使用 Windows 窗体实现 MVP 模式,我对当前的实现有疑问,因为我正试图将其融入更复杂的架构中。 现在我有一个带有属性的完全不可知的 View ,一个在构造函数中注入(inject) V
对所有 GWT 大师的问题。 我是 GWT 的新手,正在尝试了解编写 GWT 应用程序的最佳实践。我经历过"Large scale application development and MVP"基于
我最近一直试图从架构的角度来理解 MVP 架构中交互器和用例之间的关系。我的疑问是关于部件之间的通信以及符合 MVP 标准的内容。 问题是我见过很多相互矛盾的流程图。其中一些似乎每个演示者都有一个交互
我有一个具有 MVP 架构模式的应用程序。 现在,我正在尝试在我的应用程序中实现 Livedata。 我搜索了很多,但我找不到任何教程或示例。 所有教程都说实时数据适用于 MVVM 模式。 在 MVP
我有一个关于在使用 MVP 时如何处理演示者之间的通信的问题。假设我有两个 MVP-triads。一个是产品列表(Triad A),另一个是有关当前所选产品的一般信息(Triad B)。 我如何告诉
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
我读了鲍勃叔叔的《整洁架构书》。我尝试实现简单的例子。我有这个图: 我不明白如何实现红线部分。例如,我有一个简单的网页: 我有 2 个按钮、一个已编辑的文本和一个标签。如果我按下发送到服务器按钮 -
我正在查看 MVP 的 google 示例,我看到 this作为 Activity 的 onCreate 中的最后一个语句: new TaskDetailPresenter(
我是一名优秀的程序员,十分优秀!