0
votes

I have an angular service that I am writing unit tests for and one of the methods I am testing uses module variables from external dependencies that I am mocking and injecting in a BeforeEach before the test is run. These variables are either just standard arrays or JSON and in the original function, they are accessed based off the outputs off service dependency methods.

I've mocked the methods using jasmine createSpy and callFake and I tried mocking the values of these variables in the same way but they are coming back as undefined and the test fails as a result.

What would be the best approach to mock the values of these variables?

The variables are defined for example like this:

var module = {
   variableOne= [ a, b , c];
   variableTwo = {1, 2, 3};
};

and then the service method that I am testing calls them by eg:

DependentService.variableOne[outputOfMockedDependencyMethod];
1

1 Answers

2
votes

In your tests, when you inject the service, you can then change it :

it(
  'your desc',
  inject([YourService], (service: YourService) => {
    service.mockedValue = {/* all you want */};
  }));