茉莉花-“无法读取未定义引发的属性'值'”

时间:2019-09-17 12:46:02

标签: angular jasmine karma-jasmine angular-unit-test

我刚刚开始使用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;
                        })
                    }
                )
            );
    }
});

如何找到此错误的来源?有人遇到过茉莉花不一致的事吗?

1 个答案:

答案 0 :(得分:0)

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

@discardableResult func signal() -> Int