require 'test_helper' class ControllerMethodsTest < ActionController::TestCase include Apotomo::TestCaseMethods::TestController context "A Rails controller" do context "responding to #apotomo_root" do should "initially return a root widget" do assert_equal 1, @controller.apotomo_root.size end should "allow tree modifications" do @controller.apotomo_root << mouse_mock assert_equal 2, @controller.apotomo_root.size end end context "responding to #apotomo_request_processor" do should "initially return the processor which has an empty root" do assert_kind_of Apotomo::RequestProcessor, @controller.apotomo_request_processor assert_equal 1, @controller.apotomo_request_processor.root.size end end should "respond to parent_controller" do assert_equal @controller, @controller.send(:parent_controller) end context "invoking #has_widgets" do setup do @controller.class.has_widgets do |root| root << widget(:mouse, 'mum') end end should "add the widgets to apotomo_root" do assert_equal 'mum', @controller.apotomo_root['mum'].name end should "add the widgets only once in apotomo_root" do @controller.apotomo_root assert @controller.apotomo_root['mum'] end should "allow multiple calls to has_widgets" do @controller.class.has_widgets do |root| root << widget(:mouse, 'kid') end assert @controller.apotomo_root['mum'] assert @controller.apotomo_root['kid'] end should "inherit has_widgets blocks to sub-controllers" do berry = widget(:mouse, 'berry') @sub_controller = Class.new(@controller.class) do has_widgets { |root| root << berry } end.new @sub_controller.params = {} assert @sub_controller.apotomo_root['mum'] assert @sub_controller.apotomo_root['berry'] end should "be executed in controller context" do @controller.instance_eval do def roomies; ['mice', 'cows']; end end @controller.class.has_widgets do |root| root << widget(:mouse, 'kid', :display, :roomies => roomies) end assert_equal ['mice', 'cows'], @controller.apotomo_root['kid'].options[:roomies] end end context "invoking #url_for_event" do should "compute an url for any widget" do assert_equal "/barn/render_event_response?source=mouse&type=footsteps&volume=9", @controller.url_for_event(:footsteps, :source => :mouse, :volume => 9) end end end context "invoking #render_widget" do setup do @mum = mouse_mock('mum', 'eating') end should "render the widget" do @controller.apotomo_root << @mum assert_equal "