简体   繁体   English

带有中介模式的单元测试-私有对公共

[英]Unit Testing with the Mediator Pattern - All Private to Public

I am using the mediator pattern to facilitate unit testing of GUI objects. 我正在使用中介者模式来促进GUI对象的单元测试。

psudo code Example: 伪代码示例:

Class MyGuiClass
{
  //...  Declare and initialize mediator to be a MyMediator
  private void On_SomeButtonPressed()
  {
     mediator.SomeButtonWasPressed();
  }
}

Class MyMeditator
{
  public void On_SomeButtonPressed()
  {
     //.. Do something now that the button was pressed
  }

}

This is nice because I can now unit test what happens when SomeButton is pressed without having to create a Window. 很好,因为我现在可以单元测试按下SomeButton时发生的情况,而不必创建窗口。

My concern is that I have taken a method that was private and made it public for any one who makes a Mediator to call. 我担心的是,我采用了一种私有的方法,并公开给任何让调解员打电话的人。 Past times I have done this it did not bother me because I did not have many methods that I had to make public. 过去,我这样做并没有打扰我,因为我没有很多必须公开的方法。

I am currently refactoring a very large class to use this pattern and I am wondering if there is someway I can control the visibility of who can make a MyMediator or which classes some of the methods are public for. 我目前正在重构一个非常大的类以使用此模式,并且我想知道是否可以通过某种方式控制谁可以创建MyMediator或某些方法公开的类。 (This may not be possible or even needed, but I thought I would ask.) (这可能不可能甚至不需要,但是我想我会问。)

(I am using C# 3.0 with .NET 3.5 SP1) (我将C#3.0与.NET 3.5 SP1结合使用)

I think it doesn't matter.. Who has an instance of the mediator, other than the gui? 我认为这没有关系。除了gui以外,谁还有调解员的实例? If someone does, is it going to call the method? 如果有人这样做,它将调用该方法吗? If it does, does it matter? 如果有,那有关系吗? Will it be hard to notice, diagnose and fix the bug? 很难发现,诊断和修复该错误吗?

I think you can achieve what you are looking for with events though: 我认为您可以通过事件实现您想要的目标:

eg 例如

/* in the gui class (view) */
public event EventHandler OnButtonClicked;

/* in the mediator */
public MyMediator(MyView view) 
{
    view.OnButtonClicked += HandleButtonClicked;
}

private void HandleButtonClicked(object sender, EventArgs e)
{

}

Not sure about c#, but in java you can declare something as package-level access (in java by omitting the access specifier). 不确定c#,但是在Java中,您可以将某些内容声明为包级访问(在Java中,通过省略访问说明符)。 What I do is create a separate test hierarchy that parallels my package structure, so to test class com.abcMyClass, I'll have a test class com.abcMyClassTest, which can then legally access the package-access methods in MyClass. 我要做的是创建一个与我的包结构相似的单独的测试层次结构,因此要测试com.abcMyClass类,我将拥有一个测试类com.abcMyClassTest,然后可以合法地访问MyClass中的包访问方法。

I don't so much like the idea of making everything public not only because of access issues, but because it clutters up the interface - I'd rather the public interface of the class express what it does, not how it does it, which is often where I end up if I expose methods I'd prefer to be private. 我没有那么多喜欢做的一切公共的想法不仅是因为准入问题,而是因为它杂波了界面-我宁愿类的公有接口表达它做什么 ,而不是它是怎么做的,这如果我公开我更愿意私有的方法,那通常是我最终的归宿。

The point is that you'd like the public interface of a class to show that class's public 'API', so in making private methods public you are making the class more confusing and less 'clean'? 关键是,您希望一个类的公共接口显示该类的公共“ API”,因此在公开私有方法时,您是在使该类更加混乱并且减少“干净”吗?

A few things you can do: 1) think through what actually is the 'public face' of your mediator (or humble object) class and happily make those methods public. 您可以做一些事情:1)仔细考虑调解员(或卑微对象)类的“公开面孔”,并愉快地将这些方法公开。 Even if they are only used within the assembly - not part of the assembly's public face - that's okay because notice that your mediator class itself is not declared public. 即使它们仅在程序集中使用-而不是程序集的公开外观的一部分-没关系,因为请注意,您的调解器类本身未声明为公共。 So even its public methods are still internal to the assembly. 因此,即使它的公共方法仍然在程序集内部。

2) You can fudge the privates by using internal for private (and then set the assembly's InternalsVisibleTo attribute if your test classes are in a separate assembly). 2)您可以通过使用internal for private来伪造private(如果测试类位于单独的程序集中,则设置程序集的InternalsVisibleTo属性)。

3) Take the 'black box' approach to unit testing whereby in principle you never need to test the privates because they get tested via their use when called from the public methods. 3)采用“黑匣子”方法进行单元测试,原则上您不需要测试私有对象,因为从公共方法调用私有对象时,它们是通过使用它们进行测试的。

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

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