Class: Apotomo::TestCase
- Inherits:
-
Cell::TestCase
- Object
- Cell::TestCase
- Apotomo::TestCase
- Includes:
- WidgetShortcuts
- Defined in:
- lib/apotomo/test_case.rb
Overview
Testing is fun. Test your widgets!
This class helps you testing widgets where it can. It is similar as in a controller. A declarative test would look like
class BlogWidgetTest < Apotomo::TestCase
has_widgets do |root|
root << widget(:comments_widget, 'post-comments')
end
it "should be rendered nicely" do
render_widget 'post-comments'
assert_select "div#post-comments", "Comments for this post"
end
it "should redraw on :update" do
trigger :update
assert_response "$(\"post-comments\").update ..."
end
For unit testing, you can grab an instance of your tested widget.
it "should be visible" do
assert root['post-comments'].visible?
end
See also in Cell::TestCase.
Class Method Summary collapse
-
.has_widgets(&block) ⇒ Object
Setup a widget tree as you’re used to it from your controller.
- .has_widgets_blocks ⇒ Object
Instance Method Summary collapse
-
#assert_response(*content) ⇒ Object
After a #trigger this assertion compares the actually triggered page updates with the passed.
- #parent_controller ⇒ Object
-
#render_widget(*args) ⇒ Object
Renders the widget
name
. -
#root ⇒ Object
Returns the widget tree from TestCase.has_widgets.
- #setup ⇒ Object
-
#trigger(type, options) ⇒ Object
Triggers an event of
type
.
Methods included from WidgetShortcuts
Class Method Details
.has_widgets(&block) ⇒ Object
Setup a widget tree as you’re used to it from your controller. Executed in test context.
37 38 39 |
# File 'lib/apotomo/test_case.rb', line 37 def (&block) @has_widgets = block # DISCUSS: use ControllerMethods? end |
.has_widgets_blocks ⇒ Object
34 |
# File 'lib/apotomo/test_case.rb', line 34 def ; @has_widgets; end |
Instance Method Details
#assert_response(*content) ⇒ Object
After a #trigger this assertion compares the actually triggered page updates with the passed.
Example:
trigger :submit, :source => "post-comments"
assert_response "alert(\":submit clicked!\")", /\$\("post-comments"\).update/
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/apotomo/test_case.rb', line 88 def assert_response(*content) updates = root.page_updates i = 0 content.each do |assertion| if assertion.kind_of? Regexp assert_match assertion, updates[i] else assert_equal assertion, updates[i] end i+=1 end end |
#parent_controller ⇒ Object
61 62 63 |
# File 'lib/apotomo/test_case.rb', line 61 def parent_controller @controller end |
#render_widget(*args) ⇒ Object
Renders the widget name
.
66 67 68 |
# File 'lib/apotomo/test_case.rb', line 66 def (*args) @last_invoke = root.(*args) end |
#root ⇒ Object
Returns the widget tree from TestCase.has_widgets.
54 55 56 57 58 59 |
# File 'lib/apotomo/test_case.rb', line 54 def root blk = self.class. or raise "Please setup a widget tree using TestCase.has_widgets" @root ||= Apotomo::Widget.new(parent_controller, "root").tap do |root| self.instance_exec(root, &blk) end end |
#setup ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/apotomo/test_case.rb', line 42 def setup super @controller.instance_eval do def controller_path 'barn' end end @controller.extend Apotomo::Rails::ControllerMethods end |
#trigger(type, options) ⇒ Object
Triggers an event of type
. You have to pass :source
as options.
Example:
trigger :submit, :source => "post-comments"
75 76 77 78 79 80 |
# File 'lib/apotomo/test_case.rb', line 75 def trigger(type, ) source = root.(.delete(:source)) source..merge!() # TODO: this is just a try-out (what about children?). source.fire(type) root.page_updates # DISCUSS: use ControllerMethods? end |