Permalink
Browse files

Remove 'assign_to' matcher

* See issue #252
  • Loading branch information...
1 parent 47ffcdb commit 3ec4c0094f519952a19bc4a3ce9c0884adf4ebf3 @mxie mxie committed with mxie Mar 15, 2013
View
@@ -58,7 +58,6 @@ describe PostsController, "#show" do
get :show, :id => 1
end
- it { should assign_to(:user) }
it { should respond_with(:success) }
it { should render_template(:show) }
it { should_not set_the_flash }
@@ -51,15 +51,14 @@ Feature: integrate with Rails
end
should respond_with(:success)
- should assign_to(:example)
end
"""
When I set the "TESTOPTS" environment variable to "-v"
When I successfully run `bundle exec rake test --trace`
Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
- And the output should contain "2 tests, 2 assertions, 0 failures, 0 errors"
+ And the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
And the output should contain "User should require name to be set"
- And the output should contain "ExamplesController should assign @example"
+ And the output should contain "should respond with 200"
Scenario: generate a rails application and use matchers in Rspec
When I configure the application to use rspec-rails
@@ -79,13 +78,13 @@ Feature: integrate with Rails
describe ExamplesController, "show" do
before { get :show }
- it { should assign_to(:example) }
+ it { should respond_with(:success) }
end
"""
When I successfully run `bundle exec rake spec SPEC_OPTS=-fs --trace`
Then the output should contain "2 examples, 0 failures"
And the output should contain "should require name to be set"
- And the output should contain "should assign @example"
+ And the output should contain "should respond with 200"
Scenario: generate a Rails application that mixes Rspec and Test::Unit
When I configure the application to use rspec-rails in test and development
@@ -104,13 +103,12 @@ Feature: integrate with Rails
require 'test_helper'
class ExamplesControllerTest < ActionController::TestCase
- test 'assigns to @example' do
+ test 'responds successfully' do
get :show
- assert assign_to(:example).matches?(@controller)
+ assert respond_with(:success)
end
end
"""
When I successfully run `bundle exec rake spec test:functionals SPEC_OPTS=-fs --trace`
Then the output should contain "1 example, 0 failures"
And the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
- And the output should contain "should require name to be set"
@@ -1,4 +1,3 @@
-require 'shoulda/matchers/action_controller/assign_to_matcher'
require 'shoulda/matchers/action_controller/filter_param_matcher'
require 'shoulda/matchers/action_controller/set_the_flash_matcher'
require 'shoulda/matchers/action_controller/render_with_layout_matcher'
@@ -20,7 +19,6 @@ module Matchers
# get :show, :id => User.first.to_param
# end
#
- # it { should assign_to(:user) }
# it { should respond_with(:success) }
# it { should render_template(:show) }
# it { should not_set_the_flash) }
@@ -1,130 +0,0 @@
-require 'active_support/deprecation'
-
-module Shoulda # :nodoc:
- module Matchers
- module ActionController # :nodoc:
- # Ensures that the controller assigned to the named instance variable.
- #
- # Options:
- # * <tt>with_kind_of</tt> - The expected class of the instance variable
- # being checked.
- # * <tt>with</tt> - The value that should be assigned.
- #
- # Example:
- #
- # it { should assign_to(:user) }
- # it { should_not assign_to(:user) }
- # it { should assign_to(:user).with_kind_of(User) }
- # it { should assign_to(:user).with(@user) }
- def assign_to(variable)
- AssignToMatcher.new(variable)
- end
-
- class AssignToMatcher # :nodoc:
- attr_reader :failure_message_for_should, :failure_message_for_should_not
-
- def initialize(variable)
- ActiveSupport::Deprecation.warn 'The assign_to matcher is deprecated and will be removed in 2.0'
- @variable = variable.to_s
- @options = {}
- @options[:check_value] = false
- end
-
- def with_kind_of(expected_class)
- @options[:expected_class] = expected_class
- self
- end
-
- def with(expected_value = nil, &block)
- @options[:check_value] = true
- @options[:expected_value] = expected_value
- @options[:expectation_block] = block
- self
- end
-
- def matches?(controller)
- @controller = controller
- normalize_expected_value!
- assigned_value? &&
- kind_of_expected_class? &&
- equal_to_expected_value?
- end
-
- def description
- description = "assign @#{@variable}"
- if @options.key?(:expected_class)
- description << " with a kind of #{@options[:expected_class]}"
- end
- description
- end
-
- def in_context(context)
- @context = context
- self
- end
-
- private
-
- def assigned_value?
- if @controller.instance_variables.map(&:to_s).include?("@#{@variable}")
- @failure_message_for_should_not =
- "Didn't expect action to assign a value for @#{@variable}, " <<
- "but it was assigned to #{assigned_value.inspect}"
- true
- else
- @failure_message_for_should =
- "Expected action to assign a value for @#{@variable}"
- false
- end
- end
-
- def kind_of_expected_class?
- if @options.key?(:expected_class)
- if assigned_value.kind_of?(@options[:expected_class])
- @failure_message_for_should_not =
- "Didn't expect action to assign a kind of #{@options[:expected_class]} " <<
- "for #{@variable}, but got one anyway"
- true
- else
- @failure_message_for_should =
- "Expected action to assign a kind of #{@options[:expected_class]} " <<
- "for #{@variable}, but got #{assigned_value.inspect} " <<
- "(#{assigned_value.class.name})"
- false
- end
- else
- true
- end
- end
-
- def equal_to_expected_value?
- if @options[:check_value]
- if @options[:expected_value] == assigned_value
- @failure_message_for_should_not =
- "Didn't expect action to assign #{@options[:expected_value].inspect} " <<
- "for #{@variable}, but got it anyway"
- true
- else
- @failure_message_for_should =
- "Expected action to assign #{@options[:expected_value].inspect} " <<
- "for #{@variable}, but got #{assigned_value.inspect}"
- false
- end
- else
- true
- end
- end
-
- def normalize_expected_value!
- if @options[:expectation_block]
- @options[:expected_value] = @context.instance_eval(&@options[:expectation_block])
- end
- end
-
- def assigned_value
- @controller.instance_variable_get("@#{@variable}")
- end
- end
- end
- end
-end
@@ -1,66 +0,0 @@
-require 'spec_helper'
-
-describe Shoulda::Matchers::ActionController::AssignToMatcher do
- it 'includes the actual class in the failure message' do
- define_class(:WrongClass) do
- def to_s
- 'wrong class'
- end
- end
-
- controller = build_response { @var = WrongClass.new }
- matcher = assign_to(:var).with_kind_of(Fixnum)
- matcher.matches?(controller)
-
- matcher.failure_message_for_should.should =~ /but got wrong class \(WrongClass\)$/
- end
-
- context 'a controller that assigns to an instance variable' do
- it 'accepts assigning to that variable' do
- controller.should assign_to(:var)
- end
-
- it 'accepts assigning to that variable with the correct class' do
- controller.should assign_to(:var).with_kind_of(String)
- end
-
- it 'rejects assigning to that variable with another class' do
- controller.should_not assign_to(:var).with_kind_of(Fixnum)
- end
-
- it 'accepts assigning the correct value to that variable' do
- controller.should assign_to(:var).with('value')
- end
-
- it 'rejects assigning another value to that variable' do
- controller.should_not assign_to(:var).with('other')
- end
-
- it 'rejects assigning to another variable' do
- controller.should_not assign_to(:other)
- end
-
- it 'accepts assigning to the same value in the test context' do
- expected = 'value'
- controller.should assign_to(:var).in_context(self).with { expected }
- end
-
- it 'rejects assigning to the another value in the test context' do
- expected = 'other'
- controller.should_not assign_to(:var).in_context(self).with { expected }
- end
-
- def controller
- build_response { @var = 'value' }
- end
- end
-
- context 'a controller that assigns a nil value to an instance variable' do
- it 'accepts assigning to that variable' do
- controller = build_response do
- @var = nil
- end
- controller.should assign_to(:var)
- end
- end
-end

0 comments on commit 3ec4c00

Please sign in to comment.