File: active_model_integration_spec.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (79 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download
1
2
3
4
5
6
7
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'acceptance_spec_helper'

describe 'shoulda-matchers integrates with an ActiveModel project' do
  before do
    create_active_model_project

    write_file 'lib/user.rb', <<-FILE
      require 'active_model'

      class User
        include ActiveModel::Validations
        attr_accessor :gender

        validates :gender, inclusion: { in: %w(male female) }
      end
    FILE

    write_file 'spec/user_spec.rb', <<-FILE
      require 'spec_helper'
      require 'user'
      include Shoulda::Matchers::ActiveModel

      describe User do
        context 'when gender is valid' do
          it { is_expected.to validate_inclusion_of(:gender).in_array(%w(male female)) }
        end
        context 'when gender is invalid' do
          it { is_expected.to validate_inclusion_of(:gender).in_array(%w(transgender female)) }
        end
      end
    FILE

    write_file 'load_dependencies.rb', <<-FILE
      require 'active_model'
      require 'shoulda-matchers'

      puts ActiveModel::VERSION::STRING
      puts "Loaded all dependencies without errors"
    FILE

    updating_bundle do
      add_rspec_to_project
      add_shoulda_matchers_to_project(
        manually: true,
        with_configuration: false,
      )

      write_file 'spec/spec_helper.rb', <<-FILE
        require 'active_model'
        require 'shoulda-matchers'

        Shoulda::Matchers.configure do |config|
          config.integrate do |with|
            with.test_framework :rspec

            with.library :active_model
          end
        end
      FILE
    end
  end

  context 'when using active model library' do
    it 'and loads without errors' do
      result = run_command_within_bundle('ruby load_dependencies.rb')

      expect(result).to have_output('Loaded all dependencies without errors')
    end

    it 'allows use of inclusion matcher from active model library' do
      result = run_rspec_tests('spec/user_spec.rb')

      expect(result).to have_output('2 examples, 1 failure')
      expect(result).to have_output(
        'gender: ["is not included in the list"]',
      )
    end
  end
end