在Angular 8中进行ng测试时,karma控制台说;未捕获的SyntaxError:JSON中位于位置1的意外标记o抛出;
这是规范文件在Angular 8中进行ng测试时,karma控制台说;未捕获的SyntaxError:JSON中位于位置1的意外标记o抛出;,json,angular,unit-testing,jasmine,karma-runner,Json,Angular,Unit Testing,Jasmine,Karma Runner,这是规范文件 ... ... import { AccountService } from 'src/app/core/account.service'; import accountsummary from 'src/app/core/model/mock-account-summary.json'; import transactions from 'src/app/core/model/mock-transaction-history.json'; import { Accountsumm
...
...
import { AccountService } from 'src/app/core/account.service';
import accountsummary from 'src/app/core/model/mock-account-summary.json';
import transactions from 'src/app/core/model/mock-transaction-history.json';
import { AccountsummaryComponent } from './accountsummary.component';
describe('AccountsummaryComponent', () => {
let component: AccountsummaryComponent;
let fixture: ComponentFixture<AccountsummaryComponent>;
let debugElement: DebugElement;
let mockAccountService;
let mockModalService;
let accSummary;
let trans;
beforeEach(async(() => {
accSummary = accountsummary;
trans = transactions;
mockAccountService = jasmine.createSpyObj(['getAccountSummary', 'getTransactionHistory']);
mockModalService = jasmine.createSpyObj(['show']);
TestBed.configureTestingModule({
declarations: [AccountsummaryComponent],
providers: [
{ provide: AccountService, useValue: mockAccountService },
{ provide: BsModalService, useValue: mockModalService },
],
imports: [ModalModule.forRoot(), RouterTestingModule, HttpClientTestingModule],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AccountsummaryComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
});
it('clicking on info-icon should open info modal', () => {
mockAccountService.getTransactionHistory.and.returnValue(of(trans));
mockAccountService.getAccountSummary.and.returnValue(of(accSummary));
fixture.detectChanges();
const showModal = mockModalService.show.and.returnValue(of([]));
const infoIcon = debugElement.query(By.css('img'));
infoIcon.triggerEventHandler('click', {});
fixture.detectChanges();
expect(showModal).toHaveBeenCalled();
});
});
在spec文件中,我有acsumary
和trans
,它们包含各自的json数据。我正在模拟AccountService
来调用getAccountSummary
和getTransactionHistory
方法。我从这些方法返回acsumary
和trans
。但是Karma控制台说未捕获的SyntaxError:JSON中的意外标记o位于抛出的位置1
。测试随机通过,有时失败。请帮忙。
这是component.ts文件:
ngOnInit() {
this.authService.isTokenIdValid().subscribe((res) => {
}, () => {
window.localStorage.removeItem('token');
this.router.navigate(['/login'])
})
this.accountService.getTransactionHistory().subscribe((res) => {
res = JSON.parse(res);
this.transactionHistory = res['Result'].array.RevTrxn.sort((a, b) => {
return a.trxn.recDate > b.trxn.recDate ? -1 : 1;
});
this.filteredTransactions = this.transactionHistory;
this.filteredTransactionsLength = this.filteredTransactions.length;
this.currentTransactions = this.transactionHistory.slice(0, this.itemsPerPage);
this.isTransactionsLoaded = true;
this.maxDate = new Date(this.filteredTransactions[0].trxn.recDate);
this.minDate = new Date(this.maxDate.getTime() - 7 * 24 * 60 * 60 * 1000);
this.bsRangeValue = [this.minDate, this.maxDate];
this.selectedDateRange = this.bsRangeValue;
this.onDateChange(this.selectedDateRange);
})
}
您是否在组件中执行任何JSON.parse和JSON.stringify操作?@AliF50是的,我在组件的ngOnInit中使用了JSON.parse。我已在上面添加了ngOnInit代码。您是否查看了关于存在的相同错误的任何问题?它通常发生在您尝试解析和已解析的对象时。这是否回答了您的问题?
ngOnInit() {
this.authService.isTokenIdValid().subscribe((res) => {
}, () => {
window.localStorage.removeItem('token');
this.router.navigate(['/login'])
})
this.accountService.getTransactionHistory().subscribe((res) => {
res = JSON.parse(res);
this.transactionHistory = res['Result'].array.RevTrxn.sort((a, b) => {
return a.trxn.recDate > b.trxn.recDate ? -1 : 1;
});
this.filteredTransactions = this.transactionHistory;
this.filteredTransactionsLength = this.filteredTransactions.length;
this.currentTransactions = this.transactionHistory.slice(0, this.itemsPerPage);
this.isTransactionsLoaded = true;
this.maxDate = new Date(this.filteredTransactions[0].trxn.recDate);
this.minDate = new Date(this.maxDate.getTime() - 7 * 24 * 60 * 60 * 1000);
this.bsRangeValue = [this.minDate, this.maxDate];
this.selectedDateRange = this.bsRangeValue;
this.onDateChange(this.selectedDateRange);
})
}