Sha256: eb96bf7ceeee6ae8ade8fb381982729dc78674aa73073bd2ff5b9ce7e9b8c419

Contents?: true

Size: 1.26 KB

Versions: 22

Compression:

Stored size: 1.26 KB

Contents

module RSpec
  module Rails
    module Matchers
      # @private
      class BeValid < RSpec::Matchers::BuiltIn::Be
        def initialize(*args)
          @args = args
        end

        def matches?(actual)
          @actual = actual
          actual.valid?(*@args)
        end

        def failure_message
          message = "expected #{actual.inspect} to be valid"

          if actual.respond_to?(:errors)
            errors = if actual.errors.respond_to?(:full_messages)
                       actual.errors.full_messages
                     else
                       actual.errors
                     end

            message << ", but got errors: #{errors.map(&:to_s).join(', ')}"
          end

          message
        end

        def failure_message_when_negated
          "expected #{actual.inspect} not to be valid"
        end
      end

      # @api public
      # Passes if the given model instance's `valid?` method is true, meaning
      # all of the `ActiveModel::Validations` passed and no errors exist. If a
      # message is not given, a default message is shown listing each error.
      #
      # @example
      #     thing = Thing.new
      #     expect(thing).to be_valid
      def be_valid(*args)
        BeValid.new(*args)
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
rspec-rails-3.8.2 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.8.1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.8.0 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.7.2 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.7.1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.7.0 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.6.1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.6.0 lib/rspec/rails/matchers/be_valid.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/rspec-rails-3.5.2/lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.6.0.beta2 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.6.0.beta1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.2 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.0 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.0.beta4 lib/rspec/rails/matchers/be_valid.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/rspec-rails-3.4.2/lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.0.beta3 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.0.beta2 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.5.0.beta1 lib/rspec/rails/matchers/be_valid.rb
rspec-rails-3.4.2 lib/rspec/rails/matchers/be_valid.rb