mercredi 24 juin 2015

Can a stub fail a test?

Hello I have been reading "The Art of Unit Testing, 2nd Edition" by Roy Osherove and also I read Martin Fowler's essay Mocks Aren't Stubs.

The problem comes when Osherove says this: "The basic difference is that stubs can’t fail tests. Mocks can" and uses this example of a interaction testing:

[Test]
public void Analyze_TooShortFileName_CallsWebService()
{
 FakeWebService mockService = new FakeWebService();
 LogAnalyzer log = new LogAnalyzer(mockService);
 string tooShortFileName="abc.ext";
 log.Analyze(tooShortFileName);
 StringAssert.Contains("Filename too short:abc.ext",
 mockService.LastError);
}

But after reading Fowler this example looks more like a state test trying to emulate a interaction test without a using a framework. Also Fowler used the following example of traditional state test:

Fowler state test

public class OrderStateTester extends TestCase {
private static String TALISKER = "Talisker";
private static String HIGHLAND_PARK = "Highland Park";
private Warehouse warehouse = new WarehouseImpl();

protected void setUp() throws Exception {
  warehouse.add(TALISKER, 50);
  warehouse.add(HIGHLAND_PARK, 25);
}
public void testOrderIsFilledIfEnoughInWarehouse() {
  Order order = new Order(TALISKER, 50);
  order.fill(warehouse);
  assertTrue(order.isFilled());
  assertEquals(0, warehouse.getInventory(TALISKER));
}
public void testOrderDoesNotRemoveIfNotEnough() {
  Order order = new Order(TALISKER, 51);
  order.fill(warehouse);
  assertFalse(order.isFilled());
  assertEquals(50, warehouse.getInventory(TALISKER));
}

Here if I'm not wrong and following Osherove's words in Fowler's test the "warehouse" is a collaborator object but also a Mock because an assert is used agains it.

  • is the Osherove approach about interaction testing right??
  • can a Stub fail a test??

Aucun commentaire:

Enregistrer un commentaire