> For the complete documentation index, see [llms.txt](https://briebug.gitbook.io/ngrx-workshop/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://briebug.gitbook.io/ngrx-workshop/testing/testing-effects.md).

# Testing Effects

```typescript
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles';
import { Observable } from 'rxjs';

import {
  InsertUser,
  InsertUserSuccess,
  InsertUserFail
} from './user.actions';
import { generateUser, generateUserArray } from '../../core/user';
// TODO: Change this path when you move your service file:
import { UserService } from '../../core/user.service';
import { UserEffects } from './user.effects';

describe('UserEffects', () => {
  let actions: Observable<any>;
  let effects: UserEffects;
  let service;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        UserEffects,
        provideMockActions(() => actions),
        {
          provide: UserService,
          useValue: { create: jest.fn() }
        },
      ]
    });

    effects = TestBed.get(UserEffects);
    service = TestBed.get(UserService);
  });

  it('should be constructed', () => {
    expect(effects).toBeTruthy();
  });

  // Effect Test
  describe('insert', () => {
    it('should return InsertUserSuccess action with entity on success', () => {
      const entity = generateUser(),
        insertAction = new InsertUser({ user: entity }),
        successAction = new InsertUserSuccess({ result: entity }),
        response = cold('-b|', { b: entity }),
        expected = cold('-s', { s: successAction });

      actions = hot('a-', { a: insertAction });

      service.create = jest.fn(() => response);

      expect(effects.insert).toBeObservable(expected);
    });
  });

  // Effect Test - Failure Path
  it('should return InsertUserFail with error object on failure', () => {
    const entity = generateUser(),
      insertAction = new InsertUser({ user: entity }),
      failAction = new InsertUserFail({ error: 'fail' }),
      response = cold('-#|', {}, { message: 'fail' }),
      expected = cold('-f', { f: failAction });

    actions = hot('i-', { i: insertAction });

    service.create = jest.fn(() => response);

    expect(effects.insert).toBeObservable(expected);
  });
});

```

{% hint style="info" %}
Write tests for Tour of Heroes:&#x20;

<https://stackblitz.com/github/jessesanders/tour-of-heroes/tree/ngrx-entity>
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://briebug.gitbook.io/ngrx-workshop/testing/testing-effects.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
