试图模拟服务属性价值

发布于 2025-01-19 08:51:54 字数 2028 浏览 2 评论 0原文

我想模拟我的组件使用的服务属性的值。然而我似乎无法改变价值。

我遇到的问题是让组件使用我的新值。它仍然使用我的服务中的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听你说爱我 2025-01-26 08:51:54

为什么不直接更改它?

let backup = '';
beforeEach(() => backup = ConfigService.URL);
afterEach(() => ConfigService.URL = backup);

// ...

it('should check if update is available', () => {
  // ...
  ConfigService.URL = 'https://google.com';
  // ...
});

Why don't you change it directly?

let backup = '';
beforeEach(() => backup = ConfigService.URL);
afterEach(() => ConfigService.URL = backup);

// ...

it('should check if update is available', () => {
  // ...
  ConfigService.URL = 'https://google.com';
  // ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文