试图模拟服务属性价值
我想模拟我的组件使用的服务属性的值。然而我似乎无法改变价值。
我遇到的问题是让组件使用我的新值。它仍然使用我的服务中的“https://stackoverflow.com”值。
组件
import { ConfigService } from 'src/app/services/config/config.service';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.scss'],
})
export class SomeComponent implements OnInit {
newURL: string;
constructor() {}
ngOnInit() {
...
}
changeURL() {
this.newURL = ConfigService.URL;
}
}
服务
@Injectable({
providedIn: 'root',
})
export class ConfigService {
public static URL = 'https://stackoverflow.com';
}
规格
describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
const configServiceSpy = jasmine.createSpyObj(ConfigService, ['VERSION']);
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, LoggerTestingModule, IonicModule],
declarations: [SomeComponent],
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should check if update is available', () => {
component.changeURL();
expect(component.newURL).toEqual('https://stackoverflow.com');
// Now I'd like to mock this service property
configServiceSpy.URL = 'https://google.com';
component.changeURL();
expect(component.newURL).toEqual('https://google.com');
});
});
I'd like to mock the value of a service property my component uses. I can't seem to get the value to change however.
The problem I'm having is getting the component to use my new value. It's still using the 'https://stackoverflow.com' value from my service.
Component
import { ConfigService } from 'src/app/services/config/config.service';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.scss'],
})
export class SomeComponent implements OnInit {
newURL: string;
constructor() {}
ngOnInit() {
...
}
changeURL() {
this.newURL = ConfigService.URL;
}
}
Service
@Injectable({
providedIn: 'root',
})
export class ConfigService {
public static URL = 'https://stackoverflow.com';
}
Spec
describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
const configServiceSpy = jasmine.createSpyObj(ConfigService, ['VERSION']);
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, LoggerTestingModule, IonicModule],
declarations: [SomeComponent],
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should check if update is available', () => {
component.changeURL();
expect(component.newURL).toEqual('https://stackoverflow.com');
// Now I'd like to mock this service property
configServiceSpy.URL = 'https://google.com';
component.changeURL();
expect(component.newURL).toEqual('https://google.com');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接更改它?
Why don't you change it directly?