2017-04-16 59 views
0

所有參數我使用ngRedux與角2角2茉莉錯誤:無法解析NgRedux

我想測試它採用ngRedux一個簡單的組件:

import { Component, OnInit } from '@angular/core'; 
import { NgRedux, select } from 'ng2-redux'; 
import { CustomerProfileModel, rootReducer } from '../common/customerProfileReducer'; 

@Component({ 
    selector: 'app-profile-details', 
    templateUrl: './profile-details.component.html', 
    styleUrls: ['./profile-details.component.css'] 
}) 
export class ProfileDetailsComponent implements OnInit { 

    customerUIData: object; 

    constructor(
    private ngRedux: NgRedux<Map<string, any>>) { 
    this.ngRedux.subscribe(() => { 
     this.getDataFromStore() 
    }) 
    } 

    ngOnInit() { this.getDataFromStore()} 

    getDataFromStore() { 
    var store = this.ngRedux.getState(); 
    this.customerUIData = store; 
    } 

} 

我的單元測試看起來是這樣的:

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
import { NgRedux, select, NgReduxModule } from 'ng2-redux'; 
import { ProfileDetailsComponent } from './profile-details.component'; 
import { CustomerProfileModel, rootReducer } from '../common/customerProfileReducer'; 

describe('ProfileDetailsComponent',() => { 
    let component: ProfileDetailsComponent; 
    let fixture: ComponentFixture<ProfileDetailsComponent>; 
    let ngRedux: NgRedux<CustomerProfileModel>; 

    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ProfileDetailsComponent], 
     providers: [NgRedux], 
     imports:[NgReduxModule] 
    }) 
     .compileComponents(); 

    })); 

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

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

Redux店如下:

export interface CustomerProfileModel { 
    customerData: { 
     .... 
    }; 
} 

export const CUSTPROFILE_INITIAL_STATE: CustomerProfileModel = { 
    customerData: { 
     .... 
    } 
}; 

export function rootReducer(state: CustomerProfileModel, action) { 
    switch (action.type) { 
     case 'GET_CUSTOMER_PROFILE_SUCCESS': 
      return { customerData: action.customerData }; 
    } 
    return state; 
} 

我得到如下錯誤

Failed: Can't resolve all parameters for NgRedux: (?). 

誰能幫我找出哪些參數我需要把?

回答

1

刪除「提供者:[NgRedux]」,因爲如果您已經使用NgReduxModule,則不需要將NgRedux添加到您的提供者。

+0

完成!現在我得到另一個錯誤:_this._store爲空 – TnJ

+0

我不熟悉Redux,但我認爲你需要模擬「this.ngRedux.getState()」返回一個模擬庫。 – Nawsen