Module: Card::SpecHelper

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

Instance Method Summary collapse

Instance Method Details

#add_test_event(stage, name, &event_block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/card/spec_helper.rb', line 103

def add_test_event stage, name, &event_block
  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 name, event_block
  end
  Card.define_callbacks name
  Card.set_callback stage, :before, name, prepend: true
end

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



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

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

#create!(name, content = '') ⇒ Object



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

def create! name, content=''
  Card.create! name: name, content: content
end

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



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

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_stage(stage, opts = {}, &event_block) ⇒ Object

Make expectations in the event phase. Takes a stage and registers the event_block in this stage as an event. Unknown methods in the event_block are executed in the rspec context instead of the card’s context. An additionally :trigger block in opts is expected that is called to start the event phase. Other event options like :on or :when are not supported yet. Example: in_stage :initialize,

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


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/card/spec_helper.rb', line 88

def in_stage stage, opts={}, &event_block
  stage_sym = :"#{stage}_stage"
  $rspec_binding = binding
  add_test_event stage_sym, :in_stage_test, &event_block
  trigger =
    if opts[:trigger].is_a?(Symbol)
      method(opts[:trigger])
    else
      opts[:trigger]
    end
  trigger.call
ensure
  remove_test_event stage_sym, :in_stage_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

#remove_test_event(stage, name) ⇒ Object



127
128
129
# File 'lib/card/spec_helper.rb', line 127

def remove_test_event stage, name
  Card.skip_callback stage, :before, name
end

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



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

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



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

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



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

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

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



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

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



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

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

#test_event(stage, _opts, &block) ⇒ Object



131
132
133
134
135
136
# File 'lib/card/spec_helper.rb', line 131

def test_event stage, _opts, &block
  event_name = :"test_event_#{@events.size}"
  stage_sym = :"#{stage}_stage"
  @events << [stage_sym, event_name]
  add_test_event stage_sym, event_name, &block
end

#usersObject



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

def users
  SharedData::USERS.sort
end

#with_test_eventsObject



138
139
140
141
142
143
144
145
146
# File 'lib/card/spec_helper.rb', line 138

def with_test_events
  @events = []
  $rspec_binding = binding
  yield
ensure
  @events.each do |stage, name|
    remove_test_event stage, name
  end
end