# Detecting

It can often be useful to determine if you are running in a Scope.

You can detect if your code is running within a Scope by calling:

```dart
Scope.isWithinScope();

// or

isWithinScope();
```

You can also detect if a specific ScopeKey exists by calling:

```dart
if (hasScopeKey(ageKey))
{
   // change my behaviour
}

/// or

if (Scope.hasScopeKey(ageKey))
{
   // change my behaviour
}


```

Both forms of isWithinScope and hasScopeKey are identical and are provided for consistency with the two forms of the `use` method.

Detecting if you are within a Scope is really useful for unit testing.

Your unit tests can inject a ScopeKey and change your code can then change its behaviour when running within a unit test.

This can be easier then setting up a full mock framework.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://scope.onepub.dev/fundamentals/scope/detecting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
