Skip to content

Instantly share code, notes, and snippets.

@netzpirat
Created December 29, 2010 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netzpirat/758539 to your computer and use it in GitHub Desktop.
Save netzpirat/758539 to your computer and use it in GitHub Desktop.
Testing apotomo widgets with RSpec
module RSpec::Rails
module WidgetExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion
include RSpec::Rails::RailsExampleGroup
include ActionView::TestCase::Behavior
include RSpec::Rails::ViewAssigns
include RSpec::Rails::Matchers::RenderTemplate
include RSpec::Rails::BrowserSimulators
include Apotomo::WidgetShortcuts
webrat do
include Webrat::Matchers
end
module ClassMethods
def _default_helper
base = metadata[:example_group][:description].split('/').first
(base.camelize + 'Helper').constantize if base
rescue NameError
nil
end
def _default_helpers
helpers = [_default_helper].compact
helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
helpers
end
end
module InstanceMethods
# :call-seq:
# render
# render(:template => "widgets/new.html.erb")
# render({:partial => "widgets/widget.html.erb"}, {... locals ...})
# render({:partial => "widgets/widget.html.erb"}, {... locals ...}) do ... end
#
# Delegates to ActionView::Base#render, so see documentation on that for more
# info.
#
# The only addition is that you can call render with no arguments, and RSpec
# will pass the top level description to render:
#
# describe "widgets/new.html.erb" do
# it "shows all the widgets" do
# render # => view.render(:file => "widgets/new.html.erb")
# ...
# end
# end
def render(options={}, local_assigns={}, &block)
options = {:template => _default_file_to_render} if Hash === options and options.empty?
super(options, local_assigns, &block)
end
# The instance of ActionView::Base that is used to render the template.
# Use this before the +render+ call to stub any methods you want to stub
# on the view:
#
# describe "widgets/new.html.erb" do
# it "shows all the widgets" do
# view.stub(:foo) { "foo" }
# render
# ...
# end
# end
def view
_view
end
# Simulates the presence of a template on the file system by adding a
# Rails' FixtureResolver to the front of the view_paths list. Designed to
# help isolate view examples from partials rendered by the view template
# that is the subject of the example.
#
# == Example
#
# stub_template("widgets/_widget.html.erb" => "This content.")
def stub_template(hash)
view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
end
# Provides access to the params hash that will be available within the
# view:
#
# params[:foo] = 'bar'
def params
controller.params
end
# Deprecated. Use +view+ instead.
def template
RSpec.deprecate("template","view")
view
end
# Deprecated. Use +rendered+ instead.
def response
RSpec.deprecate("response", "rendered")
rendered
end
def has_widgets_blocks
@has_widgets
end
def has_widgets(&block)
@has_widgets = block
end
def root
blk = has_widgets_blocks or raise "Please setup a widget tree using TestCase.has_widgets"
@root ||= widget("apotomo/widget", "root").tap do |root|
self.instance_exec(root, &blk)
end
end
def parent_controller
controller
end
def render_widget(name, options={})
@rendered << root.find_widget(name).tap { |w| w.opts = options }.invoke
end
private
def _default_file_to_render
example.example_group.top_level_description
end
def _path_parts
_default_file_to_render.split("/")
end
def _controller_path
_path_parts[0..-2].join("/")
end
def _inferred_action
_path_parts.last.split(".").first
end
def _include_controller_helpers
helpers = controller._helpers
view.singleton_class.class_eval do
include helpers unless included_modules.include?(helpers)
end
end
end
included do
metadata[:type] = :widget
helper *_default_helpers
before do
_include_controller_helpers
controller.controller_path = _controller_path
controller.request.path_parameters["controller"] = _controller_path
controller.request.path_parameters["action"] = _inferred_action unless _inferred_action =~ /^_/
end
end
RSpec.configure do |c|
c.include self, :example_group => { :file_path => /spec\/widgets/ }
end
end
end
@netzpirat
Copy link
Author

This is a simple RSpec example group for Apotomo testing. The code is a merge of RSpec::Rails::ViewExampleGroup and Apotomo::TestCase. I merged only stuff I need, since I just started to learn Apotomo, so there is no event test code at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment