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



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

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) ⇒ Object



74
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
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 74

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

  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
  )
end

#assert_response_is_cached_by_variant(ab_test_name) ⇒ Object



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

def assert_response_is_cached_by_variant(ab_test_name)
  ab_test = AbTest.new(ab_test_name)
  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



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

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) ⇒ Object



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

def setup_ab_variant(ab_test_name, variant)
  ab_test = AbTest.new(ab_test_name)
  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
# File 'lib/govuk_ab_testing/abstract_helpers.rb', line 7

def with_variant(args)
  ab_test_name, variant = args.first

  setup_ab_variant(ab_test_name, variant)

  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)
  end
end