简体   繁体   中英

Angular test NullInjectorError: StaticInjectorError(DynamicTestModule)[UsersEffects -> Actions]:

I'm using Angular Jasmine to write uit tests. I'm getting the following error:

NullInjectorError: StaticInjectorError(DynamicTestModule)[UsersEffects -> Actions]: StaticInjectorError(Platform: core)[UsersEffects -> Actions]: NullInjectorError: No provider for Actions!

My spec.ts file is:

describe('UsersComponent', () => {
  let component: UsersComponent;
  let fixture: ComponentFixture<UsersComponent>;
  let dialogSpy: jasmine.Spy;
  let dialogRefSpyObj = jasmine.createSpyObj({ afterClosed : of({}), close: null });
  dialogRefSpyObj.componentInstance = { body: '' };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MaterialModule,
        SharedModule,
        ReactiveFormsModule,
        StoreModule.forRoot({}),
        StoreModule.forFeature('Users', reducer),
        HttpClientModule,
        UnitTestModule,
        BrowserAnimationsModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useClass: DummyTranslateLoader,
          },
        }),
      ],
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [],
      providers: [Store, UsersService],
    })
    .compileComponents();
  }));

  afterEach(() => {
    TestBed.resetTestingModule();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(UsersComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  beforeEach(() => {
    dialogSpy = spyOn(TestBed.get(MatDialog), 'open').and.returnValue(dialogRefSpyObj);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

I've also added this is app.module.ts:

StoreModule.forRoot({}),
EffectsModule.forRoot([]),

What could I be missing?

Below code resolved NullInjectorError: No provider for Actions: in my component:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Actions } from '@ngrx/effects';
import { provideMockActions } from '@ngrx/effects/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { YourComponent } from './your.component';

describe('YourComponent ', () => {
   let component: YourComponent ;
   let fixture: ComponentFixture<YourComponent>;
   let store: MockStore;
   let actions: Actions;
   beforeEach(async () => {
     await TestBed.configureTestingModule({
     declarations: [YourComponent ],
     providers: [
       provideMockStore(),
       provideMockActions(() => actions)
     ]
   })
  .compileComponents();
});

beforeEach(() => {
  fixture = TestBed.createComponent(YourComponent );
  component = fixture.componentInstance;
  store = TestBed.inject(MockStore);
  actions = TestBed.inject(Actions);
  fixture.detectChanges();
});

  it('should create YourComponent ', () => {
    expect(component).toBeTruthy();
  });
});

In your test, you should use the Mock elements provided by NGRX:

let store: MockStore<any>

...

TestBed.configureTestingModule({
   providers: [
   ...,
   provideMockStore()
   ]
})

...

store = TestBed.get(Store)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM