Skip to content

Commit

Permalink
Merge pull request #26 from beyond-the-cloud-dev/mocking-documentation
Browse files Browse the repository at this point in the history
improve mock documentation
  • Loading branch information
0ptaq0 authored May 14, 2023
2 parents ab5d097 + 5c2cb0d commit dc094e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion website/docs/basic-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ SOQL.of(Account.SObjectType).with(new List<SObjectField> {

## Mocking

To mock SOQL query use `.mockId(id)` method to make it identifiable. If you mark more than one query with the same ID, all marked queries will return the same data.
Mocking provides a way to substitute records from a Database with some prepared data. Data can be prepared in form of SObject records and lists in Apex code or Static Resource `.csv` file.
Mocked queries won't make any SOQL's and simply return data set in method definition, mock __will ignore all filters and relations__, what is returned depends __solely on data provided to the method__. Mocking is working __only during test execution__. To mock SOQL query, use `.mockId(id)` method to make it identifiable. If you mark more than one query with the same ID, all marked queries will return the same data.

```apex
public with sharing class ExampleController {
Expand Down
23 changes: 23 additions & 0 deletions website/docs/examples/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ sidebar_position: 13
Mock SOQL results in Unit Tests.
## Mock Single Record

Set mocking ID in Query declaration.

```apex
public with sharing class ExampleController {
Expand All @@ -24,6 +26,8 @@ public with sharing class ExampleController {
}
```

Pass single SObject record to SOQL class, and use mock ID to target query to be mocked.

```apex
@IsTest
public class ExampleControllerTest {
Expand All @@ -41,8 +45,13 @@ public class ExampleControllerTest {
}
}
```

During execution Selector will return record that was set by `.setMock` method.

## Mock Multiple Records

Set mocking ID in Query declaration.

```apex
public with sharing class ExampleController {
Expand All @@ -59,6 +68,7 @@ public with sharing class ExampleController {
}
}
```
Pass List of SObject records to SOQL class, and use mock ID to target query to be mocked.

```apex
@IsTest
Expand All @@ -82,8 +92,12 @@ public class ExampleControllerTest {
}
```

During execution Selector will return List of records that was set by `.setMock` method.

## Mock Count Result

Set mocking ID in Query declaration.

```apex
public with sharing class ExampleController {
Expand All @@ -99,6 +113,7 @@ public with sharing class ExampleController {
}
}
```
Pass Integer value to SOQL class, and use mock ID to target query to be mocked.

```apex
@IsTest
Expand All @@ -118,8 +133,12 @@ public class ExampleControllerTest {
}
```

During execution Selector will return Integer count that was set by `.setMock` method.

## Mock with Static Resource

Set mocking ID in Query declaration.

```apex
public with sharing class ExampleController {
Expand All @@ -137,6 +156,8 @@ public with sharing class ExampleController {
}
```

Pass String value with name of Static Resource file with `.csv` records, and use mock ID to target query to be mocked.

```apex
@IsTest
public class ExampleControllerTest {
Expand All @@ -153,3 +174,5 @@ public class ExampleControllerTest {
}
}
```

During execution Selector will return records from Static Resource that was set by `.setMock` method.
1 change: 1 addition & 0 deletions website/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ List<Account> accounts = SOQL.of(Account.SObjectType)
4. **Control sharings rules** - The selector allows to execute of different methods in the same class in different sharing modes.
5. **Avoid duplicates** - Generic SQOLs like `getById`, and `getByRecordType` can be stored in the selector class.
6. **Default configuration** - The selector class can provide default SOQL configuration like default fields, FLS settings, and sharing rules.
7. **Mocking** - The selector class has built in Mocking functionality that provides ability to dynamically return data in test execution

## License notes

Expand Down

1 comment on commit dc094e2

@vercel
Copy link

@vercel vercel bot commented on dc094e2 May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.