JSON 中的意外标记 u 在 <Jasmine> 的位置 0
Unexpected token u in JSON at position 0 at <Jasmine>
我有这个错误 'SyntaxError: Unexpected token u in JSON at position 0',模拟 cookieService 的最佳方法是什么?
我的规范文件:
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from "@angular/core";
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { FilterComponent } from "./filter.component";
import { RouterTestingModule } from '@angular/router/testing';
import { CookieModule, CookieService } from 'ngx-cookie';
import { Api } from '../../services/api.service';
describe('filter Component', () => {
let component: FilterComponent;
let fixture: ComponentFixture<FilterComponent>;
let httpMock: HttpTestingController;
let cookieService: CookieService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule,
CookieModule.forRoot()
],
declarations: [
FilterComponent,
],
providers: [
Api,
CookieService,
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(FilterComponent);
component = fixture.componentInstance;
service = TestBed.inject(Api);
httpMock = TestBed.inject(HttpTestingController);
cookieService = TestBed.inject(CookieService);
cookieService.put('user', user.username);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我的打字稿文件(构造函数)在这段代码中我调用函数cookie.get:
constructor(
private api: Api,
private cookie: CookieService,
) {
this.user = JSON.parse(this.cookie.get('user'));
}
解决方案 为了解决这个问题,只需检查您尝试解析的字符串是否为 empty/null。如果不是 empty/null 你可以解析那个字符串。
if (yourValue != null) {
output = JSON.parse(yourValue);
}
我有这个错误 'SyntaxError: Unexpected token u in JSON at position 0',模拟 cookieService 的最佳方法是什么?
我的规范文件:
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from "@angular/core";
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { FilterComponent } from "./filter.component";
import { RouterTestingModule } from '@angular/router/testing';
import { CookieModule, CookieService } from 'ngx-cookie';
import { Api } from '../../services/api.service';
describe('filter Component', () => {
let component: FilterComponent;
let fixture: ComponentFixture<FilterComponent>;
let httpMock: HttpTestingController;
let cookieService: CookieService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule,
CookieModule.forRoot()
],
declarations: [
FilterComponent,
],
providers: [
Api,
CookieService,
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(FilterComponent);
component = fixture.componentInstance;
service = TestBed.inject(Api);
httpMock = TestBed.inject(HttpTestingController);
cookieService = TestBed.inject(CookieService);
cookieService.put('user', user.username);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我的打字稿文件(构造函数)在这段代码中我调用函数cookie.get:
constructor(
private api: Api,
private cookie: CookieService,
) {
this.user = JSON.parse(this.cookie.get('user'));
}
解决方案 为了解决这个问题,只需检查您尝试解析的字符串是否为 empty/null。如果不是 empty/null 你可以解析那个字符串。
if (yourValue != null) {
output = JSON.parse(yourValue);
}