Module: GovukAbTesting::RspecHelpers

Defined in:
lib/govuk_ab_testing/rspec_helpers.rb

Instance Method Summary collapse

Instance Method Details

#acceptance_test_frameworkObject



3
4
5
6
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 3

def acceptance_test_framework
  @acceptance_test_framework ||=
    GovukAbTesting.configuration.framework_class.new(self)
end

#assert_page_not_tracked_in_ab_test(ab_test_name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 65

def assert_page_not_tracked_in_ab_test(ab_test_name)
  meta_tags =
    acceptance_test_framework.analytics_meta_tags_for_test(ab_test_name)

  expect(meta_tags).to be_empty,
    "A/B meta tag is being added to a page which should not be modified by the A/B test"
end

#assert_response_is_cached_by_variant(ab_test_name) ⇒ Object



49
50
51
52
53
54
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 49

def assert_response_is_cached_by_variant(ab_test_name)
  ab_test = GovukAbTesting::AbTest.new(ab_test_name, dimension: 123)

  vary_header_value = acceptance_test_framework.vary_header
  expect(vary_header_value).to eq(ab_test.response_header)
end

#assert_response_not_modified_for_ab_test(ab_test_name) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 56

def assert_response_not_modified_for_ab_test(ab_test_name)
  vary_header = acceptance_test_framework.vary_header

  expect(vary_header).to_not match?(/#{ab_test_name}/),
    "`Vary` header is being added to a page which should not be modified by the A/B test"

  assert_page_not_tracked_in_ab_test(ab_test_name)
end

#setup_ab_variant(ab_test_name, variant, dimension = 300) ⇒ Object



43
44
45
46
47
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 43

def setup_ab_variant(ab_test_name, variant, dimension = 300)
  ab_test = GovukAbTesting::AbTest.new(ab_test_name, dimension: dimension)

  acceptance_test_framework.set_header(ab_test.request_header, variant)
end

#with_variant(args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/govuk_ab_testing/rspec_helpers.rb', line 8

def with_variant(args)
  ab_test_name, variant = args.first
  dimension = args[:dimension]

  ab_test =
    GovukAbTesting::AbTest.new(ab_test_name.to_s, dimension: dimension)

  acceptance_test_framework.set_header(ab_test.request_header, variant)
  requested_variant = ab_test.requested_variant(acceptance_test_framework.request_headers)

  yield

  vary_header_value = acceptance_test_framework.vary_header
  expect(ab_test.response_header).to eq(vary_header_value)

  unless args[:assert_meta_tag] == false
    content = [ab_test.meta_tag_name, requested_variant.variant_name].join(':')
    ab_test_metatags =
      acceptance_test_framework.analytics_meta_tags_for_test(ab_test_name)

    expect(ab_test_metatags.count).to eq(1)

    meta_tag = ab_test_metatags.first

    expect(meta_tag.content).to eq(content)
    dimension = meta_tag.dimension

    if dimension.nil?
      expect(dimension).to_not be_nil
    else
      expect(dimension).to eq(dimension.to_s)
    end
  end
end