Module: GovukAbTesting::AbstractHelpers

Included in:
MinitestHelpers, RspecHelpers
Defined in:
lib/govuk_ab_testing/abstract_helpers.rb

Instance Method Summary collapse

Instance Method Details

#acceptance_test_frameworkObject



3
4
5
# File 'lib/govuk_ab_testing/abstract_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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 60

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

  assert_is_empty(
    enumerable: ab_test_meta_tags,
    error_message: <<-ERROR,
      Found the '#{ab_test_name}' A/B testing meta tag on a page that should not be modified by
      the A/B test.

      Check for incorrect usage of GovukAbTesting::RequestedVariant#analytics_meta_tag
    ERROR
  )
end

#assert_page_tracked_in_ab_test(ab_test_name, variant, dimension) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 75

def assert_page_tracked_in_ab_test(ab_test_name, variant, dimension)
  ab_test = AbTest.new(ab_test_name, dimension: dimension)

  ab_test_meta_tags =
    acceptance_test_framework.analytics_meta_tags_for_test(ab_test.name)

  assert_has_size(
    enumerable: ab_test_meta_tags,
    size: 1,
    error_message: <<-ERROR,
      Incorrect number of analytics meta tags on the page for A/B test '#{ab_test.name}'.
      You may need to check usage of GovukAbTesting::RequestedVariant#analytics_meta_tag in your template(s):

        <%= requested_variant.analytics_meta_tag %>

    ERROR
  )

  meta_tag = ab_test_meta_tags.first
  expected_metatag_content = "#{ab_test.meta_tag_name}:#{variant}"

  assert_is_equal(
    expected: expected_metatag_content,
    actual: meta_tag.content,
    error_message: <<-ERROR,
      The analytics meta tag content for A/B test '#{ab_test.name}' does not match the expected value.
      You may need to use GovukAbTesting::RequestedVariant#analytics_meta_tag in your template(s):

        <%= requested_variant.analytics_meta_tag %>

    ERROR
  )

  assert_not_blank(
    string: meta_tag.dimension,
    error_message: <<-ERROR,
      The meta tag dimension for the '#{ab_test_name}' A/B test is blank.
    ERROR
  )

  unless ab_test.dimension.nil?
    assert_is_equal(
      expected: ab_test.dimension.to_s,
      actual: meta_tag.dimension.to_s,
      error_message: <<-ERROR,
        The analytics meta tag for the '#{ab_test.name}' A/B test does not match the expected value.
      ERROR
    )
  end
end

#assert_response_is_cached_by_variant(ab_test_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 27

def assert_response_is_cached_by_variant(ab_test_name)
  ab_test = AbTest.new(ab_test_name, dimension: 300)
  vary_header_value = acceptance_test_framework.vary_header

  assert_contains_substring(
    string: vary_header_value,
    substring: ab_test.response_header,
    error_message: <<-ERROR,
      The 'Vary' header is not being set for the '#{ab_test.name}' A/B test.
      You will need to use GovukAbTesting::RequestedVariant#configure_response in your controller:

        requested_variant.configure_response(response)

    ERROR
  )
end

#assert_response_not_modified_for_ab_test(ab_test_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 44

def assert_response_not_modified_for_ab_test(ab_test_name)
  vary_header = acceptance_test_framework.vary_header
  assert_does_not_contain_substring(
    string: vary_header,
    substring: ab_test_name,
    error_message: <<-ERROR,
      The 'Vary' header is being set by A/B test '#{ab_test_name}' on a page that should not be modified
      by the A/B test. Check for incorrect usage of GovukAbTesting::RequestedVariant#configure_response
      in your controller.

        'Vary': #{vary_header}

    ERROR
  )
end

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



22
23
24
25
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 22

def setup_ab_variant(ab_test_name, variant, dimension = 300)
  ab_test = AbTest.new(ab_test_name, dimension: dimension)
  acceptance_test_framework.set_header(ab_test.request_header, variant)
end

#with_variant(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 7

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

  setup_ab_variant(ab_test_name, variant, dimension)

  yield

  assert_response_is_cached_by_variant(ab_test_name)

  unless args[:assert_meta_tag] == false
    assert_page_tracked_in_ab_test(ab_test_name, variant, dimension)
  end
end