Module: Card::SpecHelper

Includes:
Rails::Dom::Testing::Assertions::SelectorAssertions
Defined in:
lib/card/spec_helper.rb

Instance Method Summary collapse

Instance Method Details

#assert_view_select(view_html, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/card/spec_helper.rb', line 19

def assert_view_select view_html, *args, &block
  node = Nokogiri::HTML::Document.parse(view_html).root
  if block_given?
    assert_select node, *args, &block
  else
    assert_select node, *args
  end
end

#debug_assert_view_select(view_html, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/card/spec_helper.rb', line 28

def debug_assert_view_select view_html, *args, &block
  Rails.logger.rspec %(
    #{CodeRay.scan(Nokogiri::XML(view_html, &:noblanks).to_s, :html).div}
    <style>
      .CodeRay {
        background-color: #FFF;
        border: 1px solid #CCC;
        padding: 1em 0px 1em 1em;
      }
      .CodeRay .code pre { overflow: auto }
    </style>
  )
  assert_view_select view_html, *args, &block
end

#in_phase(opts, &event_block) ⇒ Object

Make expectations in the event phase. Takes the usual event options :on and :before/:after/:around and registers the event_block with these options as an event. Unknown methods in the event_block are executed in the rspec context instead of the card’s context. An additionaly :trigger block in opts is expected that is called to start the event phase. Example: in_phase before: :approve, on: :save,

trigger: ->{ test_card.update_attributes! content: '' } do
  expect(item_names).to eq []
end


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/card/spec_helper.rb', line 89

def in_phase opts, &event_block
  $rspec_binding = binding
  Card.class_eval do
    def method_missing m, *args
      begin
        method = eval('method(%s)' % m.inspect, $rspec_binding)
      rescue NameError
      else
        return method.call(*args)
      end
      begin
        value = eval(m.to_s, $rspec_binding)
        return value
      rescue NameError
      end
      super
#        raise NoMethodError
    end
    define_method :in_phase_test, event_block
  end
  Card.define_callbacks :in_phase_test
  kind =  ([:before, :after, :around] & opts.keys).first
  name = opts.delete(kind)
  Card.set_callback name, kind, :in_phase_test, prepend: true
  opts[:trigger].call
ensure
  Card.skip_callback name, kind, :in_phase_test
end

#login_as(user) ⇒ Object

~~~~~~~~~ HELPER METHODS ~~~~~~~~~~~~~~~#



7
8
9
10
11
12
# File 'lib/card/spec_helper.rb', line 7

def  user
  Card::Auth.current_id = (uc = Card[user.to_s]) && uc.id
  return unless @request
  @request.session[:user] = Card::Auth.current_id
  # warn "(ath)login_as #{user.inspect}, #{Card::Auth.current_id}, #{@request.session[:user]}"
end

#newcard(name, content = '') ⇒ Object



14
15
16
17
# File 'lib/card/spec_helper.rb', line 14

def newcard name, content=''
  #FIXME - misleading name; sounds like it doesn't save.
  Card.create! name: name, content: content
end

#render_card(view, card_args = {}, format_args = {}) ⇒ Object



58
59
60
# File 'lib/card/spec_helper.rb', line 58

def render_card view, card_args={}, format_args={}
  render_card_with_args view, card_args, format_args
end

#render_card_with_args(view, card_args = {}, format_args = {}, view_args = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/card/spec_helper.rb', line 62

def render_card_with_args view, card_args={}, format_args={}, view_args={}
  card = begin
    if card_args[:name]
      Card.fetch card_args[:name], new: card_args
    else
      Card.new card_args.merge(name: 'Tempo Rary')
    end
  end
  card.format(format_args)._render(view, view_args)
end

#render_content(content, format_args = {}) ⇒ Object



48
49
50
# File 'lib/card/spec_helper.rb', line 48

def render_content content, format_args={}
  render_content_with_args content, format_args
end

#render_content_with_args(content, format_args = {}, view_args = {}) ⇒ Object



52
53
54
55
56
# File 'lib/card/spec_helper.rb', line 52

def render_content_with_args content, format_args={}, view_args={}
  @card ||= Card.new name: 'Tempo Rary 2'
  @card.content = content
  @card.format(format_args)._render :core, view_args
end

#render_editor(type) ⇒ Object



43
44
45
46
# File 'lib/card/spec_helper.rb', line 43

def render_editor type
  card = Card.create(name: "my favority #{type} + #{rand(4)}", type: type)
  card.format.render(:edit)
end

#usersObject



73
74
75
# File 'lib/card/spec_helper.rb', line 73

def users
  SharedData::USERS.sort
end