簡體   English   中英

茉莉花-“無法讀取未定義引發的屬性'值'”

[英]Jasmine - “Cannot read property 'values' of undefined thrown”

我剛開始使用Jasmine編寫現有Angular應用程序的單元測試,大約50%的時間里,我收到以下錯誤;另外50%的時間里,所有測試都通過了,沒有任何問題。

此錯誤消息的主要問題是茉莉花沒有告訴我問題出在哪里

 An error was thrown in afterAll
  Uncaught TypeError: Cannot read property 'values' of undefined thrown
describe('deal without Angular testing support', () => {
    let service: DealService;
    let httpMock: HttpTestingController;

    beforeEach(async() => {

        TestBed.configureTestingModule({
            providers: [
                DealService
            ],
            imports: [HttpClientTestingModule],
            schemas: [ NO_ERRORS_SCHEMA ]
        });

        service = TestBed.get(DealService);
        httpMock = TestBed.get(HttpTestingController);
    });

    it('should use ValueService', () => {
        expect(service).toBeTruthy();
    });


    it('should return the json for deal http request', () => {
        service.getDealParameters({name:'sample name',value: "sample value"}).subscribe(data => {
            expect(data[0].dealParameterId).toBe(111);
            spyOn(service , "getDealParamDatafromSubject").and.callThrough();
            expect(service.getDealParamDatafromSubject).toHaveBeenCalled();
        });

        const req = httpMock.expectOne('https://localhost:44382/DPasdarameterSetup/GealParameters', 'call to api');
        expect(req.request.method).toBe('GET');

        req.flush([{
            dealParameterId: 111,
            actionComments: 'lorem',
            value: "sample value 1"

        },
        {
            dealParameterId: 222,
            actionComments: 'lorem',
            value: "sample value 2"

        }]);
        httpMock.verify(); 
    });
});

service code:
-----------------
export class DealParameterSetupService {
    scope: CScope[];
    primaryRestriction: CprimaryRestriction[];
    paramType: CparameterType[];
    secondaryRestriction: CSecondaryRestriction[];
    dealparams: CDealParameterSetup;
    dealParamData = new BehaviorSubject<any>('');
    dealData: Observable<any>;
    test = [];

    // Store the deserialize dealdata
    getDealParamDatafromSubject(dealData: any) {
        this.dealParamData.next(dealData);
    }

    constructor(private http: HttpClient) {
        this.dealData = this.dealParamData.asObservable();
    }

    // getDealParameters to get deal data
    getDealParameters(userDetailsObj): Observable<CDealParameterSetup[]> {
        return this.http.get<any>(environment.appBaseUrl + 'DPasdarameterSetup/GetDealParameters')
            .pipe(
                tap(
                    (data) => {
                        this.test = [];
                        data.map((dealData: CDealParameterSetup) => {
                            dealData.actionPerformedBy = userDetailsObj.userName;
                            this.test.push(new CDealParameterSetup().deserialize(dealData));
                            this.getDealParamDatafromSubject(this.test);
                            return this.test;
                        })
                    }
                )
            );
    }
});

我如何找到此錯誤的來源? 有人遇到過茉莉花不一致的地方嗎?

這可能是隨機運行測試的情況。 在茉莉3中,按隨機順序運行測試現在顯然是默認設置。 通過將random設置為false可以解決此問題。

  config.set({
    client: {
      jasmine: {
        random: false
      }
    }
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM