replaceForTesting method

void replaceForTesting(
  1. PotObjectFactory<T> factory
)
inherited

Replaces the factory set in the constructor with a new one, and/or creates a new object using the new factory, for testing purposes.

ReplaceablePot.replace is another method for replacements, and in fact, it works exactly the same way, but it is not available to pots created by the default constructor of Pot. It is because it should be safer to not have access to a feature that is not needed usually.

However, it may be necessary in tests, which are where replaceForTesting comes in handy. Set Pot.forTesting to true to use the method, but only when it is really necessary.

final counterPot = Pot(() => Counter(0));

void main() {
  Pot.forTesting = true;

  test('Counter test', () {
    counterPot.replaceForTesting(() => Counter(100));
  });
}

Note that pots created by Pot.replaceable can use this method regardless of whether or not Pot.forTesting is enabled.

For details on how this method is used and what occurs in the process of a replacement, see the document of ReplaceablePot.replace.

Implementation

void replaceForTesting(PotObjectFactory<T> factory) {
  if (!Pot.forTesting && this is! ReplaceablePot) {
    throw PotReplaceError();
  }

  _replace(factory);
}