Is the self-control pattern a violation of the principle of single responsibility?

A few years ago I used a sample of self-observation testing. When I explained this to someone recently, they claimed that he violated the PSA. The argument is that the test class can now be changed for one of two reasons: when the test changes or when the method signature on the interface that implements the test changes. After thinking about this for a while, it seems that this is the correct assessment, but I wanted to get other people's opinions. Thoughts?

Link: http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf

+5
source share
4 answers

, SRP, SRP. .

, SRP, - . , , .

PDF:

public class ScannerTest extends TestCase implements Display
{
  public ScannerTest (String name) {
    super (name);
  }
  public void testScan () {
    // pass self as a display
    Scanner scanner = new Scanner (this);
    // scan calls displayItem on its display
    scanner.scan ();
    assertEquals (new Item ("Cornflakes"), lastItem);
  }
  // impl. of Display.displayItem ()
  void displayItem (Item item) {
    lastItem = item;
  }
  private Item lastItem;
}

Mock:

public class DisplayMock implements Display
{
  // impl. of Display.displayItem ()
  void displayItem (Item item) {
    lastItem = item;
  }

  public Item getItem() {
     return lastItem;
  }
  private Item lastItem;
}

public class ScannerTest extends TestCase
{
  public ScannerTest (String name) {
    super (name);
  }
  public void testScan () {
    // pass self as a display
    DisplayMock dispMock = new DisplayMock();
    Scanner scanner = new Scanner (dispMock );
    // scan calls displayItem on its display
    scanner.scan ();
    assertEquals (new Item ("Cornflakes"), dispMock.GetItem());
  }
}

() TestClass - DisplayMock , SRP TestClass. , .

. . Agile Principles, Patterns, #. :

alt text

, , SRP ( ), . , , ( ) .

+5

-, , .

, , .

? , , . , , , . ( , , , - ). , ?

, . ? , , , , , . - ? , , , . SRP , .

+3

, , , , . SRP.

+1
source

I prefer to have a little more control over the mocks / stubs that I create. When I tried to use my own shunt template, I ended up making my test class more complex. By creating mocks as locals in the test method, I got cleaner code.

FWIW, if you are not using something powerful, such as C # (or python or the equivalent), your test code will change when you change the interface.

+1
source

All Articles