首页 > 解决方案 > Jasmine - “无法读取未定义抛出的属性‘值’”

问题描述

我刚开始使用 Jasmine 为现有的 Angular 应用程序编写单元测试,大约 50% 的时间,我收到以下错误。另外 50% 的时间,所有测试都通过,没有任何问题。

我遇到此错误消息的主要问题是 Jasmine 没有告诉我问题出在哪里

 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;
                        })
                    }
                )
            );
    }
});

我怎么能找到这个错误来自哪里?有人在 Jasmine 中遇到过任何不一致的情况吗?

标签: angularjasminekarma-jasmineangular-unit-test

解决方案


这可能是随机运行测试的情况。在 jasmine 3 中,以随机顺序运行测试显然现在是默认设置。通过将 random 设置为 false 可以解决此问题。

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

推荐阅读