Skip to content

Instantly share code, notes, and snippets.

@solnic
Created September 24, 2010 12:48
Show Gist options
  • Save solnic/595297 to your computer and use it in GitHub Desktop.
Save solnic/595297 to your computer and use it in GitHub Desktop.
describe PostsController do
describe "GET index" do
subject { get :index }
it { should assign_to(:posts).with(Post.all) }
end
end
RSpec::Matchers.define :assign_to do |name|
chain :with do |obj|
@obj = obj
end
match do |response|
ivar = "@#{name}"
result = controller.instance_variable_defined?(ivar)
if @obj
@assigned = controller.instance_variable_get(ivar)
result && @obj == @assigned
else
result
end
end
failure_message_for_should do |response|
message = "#{controller.class} should assign to :#{name}"
if @obj
message << " with #{@obj.inspect} while it was #{@assigned.inspect}"
end
message
end
failure_message_for_should_not do |response|
"#{controller.class} should not assign to :#{name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment