Created
September 24, 2010 12:48
-
-
Save solnic/595297 to your computer and use it in GitHub Desktop.
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
describe PostsController do | |
describe "GET index" do | |
subject { get :index } | |
it { should assign_to(:posts).with(Post.all) } | |
end | |
end |
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
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