Unit test – whether the self-extracting test mode violates a single responsibility principle?

Over the years I have used the self-diverting unit test pattern several times. As I explained to someone recently, they think it violates SRP. The parameters are now available for the following two reasons One to change the test class: when the test changes, or when the method signature on the interface being implemented by the test changes. After thinking about it for a period of time, it seems that this is a correct assessment, but I want to get other people’s opinions. Thinking?

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

My opinion on this is that the test class technically violates SRP, but it does not violate the spirit of SRP. The alternative to self-diversion is to combine a mock class with a test class Separate.

Using a separate mock class, you might think that it is all self-contained and satisfies the SRP, but the semantic coupling with the properties of the mock class still exists. So, in fact, we Did not achieve any meaningful separation.

Take PDF as an example:

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);
}< br /> // impl. of Display.displayItem ()
void displayItem (Item item) {
lastItem = item;
}
private Item lastItem;
}

Now we do a simulation:

public class DisplayMock implements Display
{
// impl. of Displa y.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());
}
}

Actually (IMHO) the higher coupling between TestClass and DisplayMock is worse than the SRP that violates TestClass. In addition, using the simulation framework, this problem completely disappeared.

Edit I just briefly mentioned the self-distribution model in Robert C. Martin’s excellent book Agile Principles, Patterns, and Practices in C#. Here is a summary of the book:

Therefore , The creator of SRP (discussed in detail in the same book) has no doubts about using the self-diverting model. In view of this, I will say that you are very safe against OOP (Objected Orientated Police) when using this model.

I have used the self-diverting unit test mode several times over the years. As I explained to someone recently, they think Because it violates the SRP. The parameter is now possible to change the test class for one of the following two reasons: when the test changes, or when the method signature on the interface being implemented by the test changes. After thinking about it for a while, it seems that this is A correct assessment, but I want to get other people’s opinions. Thinking?

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

My opinion on this is that the test class technically violates the SRP, but it does not violate the spirit of the SRP. The alternative to self-diversion is to separate a mock class from the test class.

Using a separate mock class, you might think that it is all self-contained and satisfies SRP, but the semantic coupling with the properties of the mock class still exists. So, in fact, we did not achieve any meaningful separation.

Take PDF as an example:

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;
}

Now we do a simulation :

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

public Item getItem() {
return lastItem;
}
private Item lastItem;
}< br />
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());
}
}

Actually (IMHO) The higher coupling between TestClass and DisplayMock is worse than the violation of TestClass’s SRP. In addition, with the use of the simulation framework, this problem has completely disappeared.

Edit I just wrote the excellent book Agile by Robert C. Martin Principles, Patterns, and Practices in C# briefly mention the self-division model. The following is a summary of the book:

Therefore, the person who created SRP (discussed in detail in the same book) did not use self-division Concerns about streaming mode. In view of this, I will say that you are very safe against OOP (Objected Orientated Police) when using this mode.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2341 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.