Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Vanity not respecting test choice in Rspec #343

Open
jennykortina opened this issue Apr 20, 2018 · 4 comments
Open

Vanity not respecting test choice in Rspec #343

jennykortina opened this issue Apr 20, 2018 · 4 comments

Comments

@jennykortina
Copy link

jennykortina commented Apr 20, 2018

I am trying to write a controller test in Rspec where I am forcing Vanity to choose a value, but the forced value is not respected

before do
    Vanity.context = controller
     Vanity.playground.experiment(key).chooses(value)
 end
@phillbaker
Copy link
Collaborator

Hi there! Thanks for using vanity, sorry to see you're having an issue.

There are some similar examples of using this helper in the vanity test suite:

experiment(:pie_or_cake).chooses(:pie)

(where experiment is just

vanity/test/test_helper.rb

Lines 132 to 134 in bc0205d

def experiment(name)
Vanity.playground.experiment(name)
end

What are the value of the variables key and value you're passing in? Can you include how you're configure the experiment?

@jennykortina
Copy link
Author

Hey,

Thanks for the quick reply!

One question I have based on your response is why are you using a symbol instead of a string to pass which experiment you are choosing:

experiment(:pie_or_cake).chooses(:pie)

In all the examples I've seen pass a string as the choice: #199

Does it need to be passed a symbol to work?

In any case this is what I did: the method I'm trying to test is (which is a private method in ApplicationController for Rails):

def _vanity_experiment_alternative(experiment)
  cookie = cookies[:experiments] && JSON.parse(cookies[:experiments])&.dig(experiment.to_s)
 
  if !cookie
    vanity_test_alternative = Vanity.ab_test(experiment)
    cookies[:experiments] = {:value => JSON.generate({experiment => vanity_test_alternative})}
    vanity_test_alternative
  else
    cookie
  end
end

I want two write two tests, one that the cookie gets set if not set, and the other that it does not get set if it's already present.

I don't have the exact test I had written anymore but it's something like the following:

describe '_vanity_experiment_alternative' do
  key = :jk_test
  value = 'i am a great value'

  before do
    Vanity.context = Struct.new(:vanity_identity).new(user.id)
    Vanity.playground.experiment(key).chooses(value)
  end

  it 'sets the cookie if cookie does not exist' do
    get :index

    expect(cookies['experiments']).to eq JSON.generate(key => value)
  end
end

The above test would not reliably return the value in the cookie that I was forcing Vanity to choose. I had to end up testing it like this which is not ideal

describe '_vanity_experiment_alternative' do
  it 'sets the cookie if cookie does not exist' do
    key = :jk_test
    get :index
    expect(['i am a great value', 'i am the worst value']).to include JSON.parse(cookies['experiments'])[key.to_s]
  end
end

Any help would be appreciated I was digging around the issues/source code for a while and couldn't figure out why it wasn't getting set.

@jennykortina
Copy link
Author

@phillbaker I just tested again using a symbol instead of a string for the value and it still does not work.

@phillbaker
Copy link
Collaborator

@jennykortina sorry for the slow reply here, what version of rails were you using?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants