S
S
sanex33392015-07-30 11:13:07
JavaScript
sanex3339, 2015-07-30 11:13:07

How can I change the value of a variable to something else in Jasmine tests inside the method under test?

Problem - I'm testing a method in which the value of one variable is equal to the property of a static class (es6),
despite the fact that Settings.urlParams themselves are created after calling the static init() method of the Settings class, the init() method is called in a completely different place.

.....
let urlparams = Settings.urlParams;
.....

Now, when testing, if you import into the spec with the test, in addition to the class being tested, also the Settings class, call Settings.init () in beforeEach, then during the method test, the test passes.
How can I make a fake Settings class so that in the method under test the value of the variable is taken from the property of the fake class?
I tried to create the Settings class inside the file with the test, with this property set in the init () method. In the beforeEach test, I call Settings.init(), but in the test, the value of the variable does not see my fake class, although if I console.log in beforeEach, the data will be displayed in the fake class, which is logical.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-07-30
@k12th

the init() method is called in a completely different place altogether

Tests can only cover what has an explicit and reproducible state.
No way. At the moment of declaring a function that uses Settings, a closure occurs, and the function retains a reference to exactly the value of the external variable Settings, which was at the time of the declaration. You can get around this, but I won’t tell you how, otherwise you’ll do such a thing that your mother is dear :)
But in general, your architecture is kind of strange. A class that has a static init method smells very strange. Singleton, right? If yes, then drop the bullshit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question