Created
May 14, 2013 20:02
-
-
Save atwam/5579015 to your computer and use it in GitHub Desktop.
Fixing rspec view specs to use inherited_resources and inherited views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ViewResourceMacros | |
def has_resource(name, &block) | |
before do | |
# Creates the resource | |
@resource ||= yield | |
# Assign to the symbol we wanted, so it's available in the view | |
assign(name, @resource) | |
# Assigns to @name so that we can use that in our assertions | |
instance_variable_set("@#{name}", @resource) | |
# If we pass an array, it's for stubing a collection, if not it's for stubbing a single object | |
if @resource.is_a?(Array) | |
view.stub(:collection) { @resource } | |
view.stub(:resource_class) {@resource.first.class} | |
else | |
view.stub(:resource) {@resource} | |
view.stub(:resource_class) {@resource.class} | |
end | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.extend ViewResourceMacros, :type => :view | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment