Reputation: 145
In my ANGULAR 2 application there is a service; that contains a static reference property.
export class MyService {
public static OneProperty: ClassA;
}
And here is a sample implementation of the method I am going to test in my ts file.
export class TestComponent() {
oneProperty: User;
public NumberOfUsers(): string {
this.oneProperty = MyService.OneProperty.someThing;
return this.oneProperty;
}
}
How can I mock the data in the static variable for the test cases? Whenever I try to mock the data in useClass/useValue format, I am getting a undefined value for MyService.OneProperty.
Upvotes: 5
Views: 3336
Reputation: 3276
Can you please post an example of how did you try useClass/useValue?
This should work:
class MockMyService {
public static oneProperty = 'mock';
}
Then in your tests:
describe('TestComponent', () => {
beforeEach(() => TestBed.configureTestingModule({
...
providers: [
{provide: MyService, useClass: MockMyService},
]
});
it('should work with mock service', () => {
const mockService = TestBed.get(MyService);
expect(mockService.oneProperty).toEqual('mock');
});
});
The above is just pseudo-like, I couldn't test it anywhere, but this should give you an idea.
Upvotes: 2