Because Project#licensed_feature_available? is strong_memoized, sometimes stub_licensed_features does not do anything because License.feature_available?(feature) will no longer be called.
See the following discussion from !9970 (merged) where this happens:
Thong Kuahchanged title from stub_licensed_features does not work sometimes due to memoization to {+stub_licensed_features+} does not work sometimes due to memoization
changed title from stub_licensed_features does not work sometimes due to memoization to {+stub_licensed_features+} does not work sometimes due to memoization
I think for this one it's probably because let!(:lock) which runs before stub_licensed_features. We can maybe fix it by using just let, but then we'll also need to update before which does project.add_developer(current_user).
I am not sure if there can be a better fix than "not memoizing" that in the tests. If we can have a very_first_before that can help.
Although very desirable the overall ~"Category:License Compliance" work is lower priority than other asks at this time and will be delayed. See gitlab-org/secure/general#65 (closed) for more information.
@NicoleSchwartz I don't think this is related to ~"Category:License Compliance". This issue is related to how we stub GitLab licensed features and not software licenses associated with detected dependencies.
I think with let_it_be we can also use let_it_be_with_refind (let_it_be_with_reload won't work):
describe'memoization'dolet_it_be_with_refind(:project){create(:project)}subject{project.feature_available?(:epics)}context'license available'dobeforedostub_licensed_features(epics: true)endit{is_expected.toeqtrue}endcontext'license not available'doit{is_expected.toeqfalse}endend
Just burned a few hours on this Both workarounds (clear_memoization and let_it_be_with_refind) fixed this I'll probably go with let_it_be_with_refind for now.
Since !54215 (merged) has been merged we no longer need to worry about clearing projects' license feature caches in specs. It's now done transparently.
@splattael I just ran into this issue again, but with a group. WDYT of doing the same thing you did in !54215 (merged) but for groups? (Patching Group#licensed_feature_available?)
With !54215 (merged) we bypass memoization for licensed_feature_available. The spec helper with_license_feature_cache be used if relying on feature cache is required.