Sha256: a09332bbad67d1e35d2df07f399112602dd6019a5c53213477182d4e456ff5d3

Contents?: true

Size: 1.67 KB

Versions: 12

Compression:

Stored size: 1.67 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActionController # :nodoc:

      # Ensures a controller responded with the expected json.
      def respond_with_json(status)
        RespondWithJsonMatcher.new(status)
      end

      class RespondWithJsonMatcher # :nodoc:

        def initialize(expected_json)
          @no_values = false
          @ignored = []
          @expected = expected_json
        end

        def ignoring_attributes(ignored = ['id', 'updated_at', 'created_at'])
          @ignored = ignored
          self
        end

        def ignoring_values()
          @no_values = true
          self
        end

        def matches?(controller)
          @actual = controller.response.body

          # remove the selected attributes
          @ignored.each do |attr|
            @actual.gsub!(/.#{attr}.:[^{,]*[,]+/, "")
            @expected.gsub!(/.#{attr}.:[^{,]*[,]+/, "")
          end
          # remove the values of all attributes
          if @no_values
            @actual.gsub!(/:[^{,]*/, "")
            @expected.gsub!(/:[^{,]*/, "")
          end

          @actual == @expected
        end

        def failure_message
          s = "Expected the following json strings to be equal: \n"
          s += "Expected: " + @expected.inspect + "\n"
          s += "     Got: " + @actual.inspect
          s
        end

        def negative_failure_message
          s = "Expected the following json strings to be different: \n"
          s += "Expected: " + @expected.inspect + "\n"
          s += "     Got: " + @actual.inspect
          s
        end

        def description
          "respond with a defined json string"
        end

      end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bigbluebutton_rails-1.3.0.mweb1 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.4.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.4.0.beta1 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.3.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.3.0.beta1 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.2.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.1.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-1.0.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-0.1.1 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-0.1.0 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-0.0.6 spec/support/matchers/shoulda/respond_with_json_matcher.rb
bigbluebutton_rails-0.0.5 spec/support/matchers/shoulda/respond_with_json_matcher.rb