Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - Store the stack for a when creation, so we can know which one isn't complete. 🪧 #238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/mocktail/lib/src/mocktail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ class MissingStubError extends Error {

typedef _ReturnsCannedResponse = Expectation<dynamic> Function();

class StubStateError extends StateError {
final _WhenCall? whenCall;

StubStateError(String message, {this.whenCall}) : super(message);

String toString() {
var wc = whenCall;
var out = "Bad state: $message";

if (wc == null) {
return out;
}

final type = wc.mock.runtimeType;
final member = wc.whenInvocation.memberName;
out +=
", Mock: ${type}, MemberName: ${member}, RegistrationStack:\n${wc.stack}";
return out;
}
}

/// Create a stub method response.
///
/// Call a method on a mock object within the call to `when`, and call a
Expand All @@ -203,7 +224,8 @@ typedef _ReturnsCannedResponse = Expectation<dynamic> Function();
/// See the README for more information.
When<T> Function<T>(T Function() x) get when {
if (_whenCall != null) {
throw StateError('Cannot call `when` within a stub response');
throw StubStateError('Cannot call `when` within a stub response',
whenCall: _whenCall);
}
_whenInProgress = true;
return <T>(T Function() fn) {
Expand Down Expand Up @@ -271,8 +293,9 @@ class When<T> {
}

class _WhenCall {
_WhenCall(this.mock, this.whenInvocation);
_WhenCall(this.mock, this.whenInvocation) : stack = StackTrace.current;

final StackTrace stack;
final Mock mock;
final Invocation whenInvocation;

Expand Down