mock_exceptions
0.8.2, released 4 years ago by wafrat.com
Allows throwing exceptions on specified method calls in fakes
What it does
Provides a small API to configure a fake to throw an exception when a particular method, getter or setter is invoked. It works by checking the invocation at the start of the fake’s implementation and throwing if a match is found.
AI-written summary of version 0.8.2, based on the package's README and metadata. Report an inaccuracy
Use it when
- Testing fakes that need to simulate error conditions
- When you want to keep the fake’s real logic but occasionally trigger failures
- When you need fine‑grained control over which calls throw
Skip it when
- Using full mocking frameworks like Mockito for stubbing and verification
- When you need to return values or capture arguments, not just throw
Strengths
- Very small dependency footprint
- Focused only on exception throwing, no extra features
- Works with any fake implementation
- Simple, readable API
Weaknesses
- Only supports throwing, not stubbing return values
- Requires manual insertion of maybeThrowException in each fake method
- Not a full mocking solution
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 1
Add maybeThrowException(this, Invocation.method(...)) to the start of each fake method you want to guard. No build_runner or permissions needed.
final f = MyFake();
whenCalling(Invocation.method(#doSomething, ['fun']))
.on(f)
.thenThrow(Exception());
expect(() => f.doSomething('fun'), throwsException);Alternatives
Packages that do the same job and could replace mock_exceptions.
Works with
Packages commonly used together with mock_exceptions: ones it builds on, ones built on it, and its own family.
- matchermock_exceptions depends on itSupport for specifying test expectations via an extensible Matcher class.
- fake_cloud_firestoreBuilt on mock_exceptionsFake implementation of Cloud Firestore for unit testing.
- firebase_auth_mocksBuilt on mock_exceptionsMocks for Firebase Auth to enable unit testing of authentication flows.
- google_sign_in_mocksBuilt on mock_exceptionsMocks for Google Sign In to write unit tests for authentication.