Sha256: 538c2a2e6804f5a33053adaa9cbb742346e68834b1eaebfe63537bfd08567e59

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module CouchPotato
  module RSpec
    class ListAsProxy
      def initialize(results_ruby)
        @results_ruby = results_ruby
      end

      def as(expected_ruby)
        ListAsMatcher.new(expected_ruby, @results_ruby)
      end
    end

    class ListAsMatcher
      include ::RSpec::Matchers::Composable

      def initialize(expected_ruby, results_ruby)
        @expected_ruby = expected_ruby
        @results_ruby = results_ruby
      end

      def matches?(view_spec)
        js = <<-JS
          (function() {
            var results = #{@results_ruby.to_json};
            var listed = '';
            var list = #{view_spec.list_function};

            var getRow = function() {
              return results.rows.shift();
            };
            var send = function(text) {
              listed = listed + text;
            };
            list();
            return JSON.stringify(JSON.parse(listed));
          })()

        JS
        @actual_ruby = JSON.parse(ExecJS.eval(js))

        values_match? @expected_ruby, @actual_ruby
      end

      def failure_message
        "Expected to list as #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
      end

      def failure_message_when_negated
        "Expected to not list as #{@expected_ruby.inspect} but did."
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch_potato-rspec-4.1.0 lib/couch_potato/rspec/matchers/list_as_matcher.rb
couch_potato-rspec-4.0.2 lib/couch_potato/rspec/matchers/list_as_matcher.rb