| |   |
| 2 | 2 | |
| 3 | 3 | describe PeopleHelper do |
| 4 | 4 | it "should say hello" do |
| 5 | | say_hello.should == "Hello" |
| 5 | helper.say_hello.should == "Hello" |
| 6 | 6 | end |
| 7 | 7 | |
| 8 | 8 | it "should provide person address text field tag" do |
| 9 | | @person = Person.new |
| 10 | | @person.address = "The moon" |
| 11 | | person_address_text_field.should == "<input id=\"person_address\" name=\"person[address]\" size=\"30\" type=\"text\" value=\"The moon\" />" |
| 12 | | end |
| 13 | | |
| 14 | | it "does not collide with PrototypeHelper" do |
| 15 | | @person = Person.new |
| 16 | | @person.address = "The moon" |
| 17 | | person_address_input_field.should == "<textarea cols=\"40\" id=\"person_address\" name=\"person[address]\" rows=\"20\">The moon</textarea>" |
| 18 | | proc do |
| 19 | | i_dont_exist |
| 20 | | end.should raise_error(NameError) |
| 9 | assigns[:person] = Person.new(:address => "The moon") |
| 10 | helper.person_address_text_field.should == "<input id=\"person_address\" name=\"person[address]\" size=\"30\" type=\"text\" value=\"The moon\" />" |
| 21 | 11 | end |
| 22 | 12 | end |
| toggle raw diff |
--- a/example_rails_app/spec/helpers/people_helper_spec.rb
+++ b/example_rails_app/spec/helpers/people_helper_spec.rb
@@ -2,21 +2,11 @@ require File.dirname(__FILE__) + '/../spec_helper'
describe PeopleHelper do
it "should say hello" do
- say_hello.should == "Hello"
+ helper.say_hello.should == "Hello"
end
it "should provide person address text field tag" do
- @person = Person.new
- @person.address = "The moon"
- person_address_text_field.should == "<input id=\"person_address\" name=\"person[address]\" size=\"30\" type=\"text\" value=\"The moon\" />"
- end
-
- it "does not collide with PrototypeHelper" do
- @person = Person.new
- @person.address = "The moon"
- person_address_input_field.should == "<textarea cols=\"40\" id=\"person_address\" name=\"person[address]\" rows=\"20\">The moon</textarea>"
- proc do
- i_dont_exist
- end.should raise_error(NameError)
+ assigns[:person] = Person.new(:address => "The moon")
+ helper.person_address_text_field.should == "<input id=\"person_address\" name=\"person[address]\" size=\"30\" type=\"text\" value=\"The moon\" />"
end
end |
| |   |
| 6 | 6 | TINY = 3 |
| 7 | 7 | RELEASE_CANDIDATE = nil |
| 8 | 8 | |
| 9 | | BUILD_TIME_UTC = 20080216153110 |
| 9 | BUILD_TIME_UTC = 20080218031844 |
| 10 | 10 | |
| 11 | 11 | STRING = [MAJOR, MINOR, TINY].join('.') |
| 12 | 12 | TAG = "REL_#{[MAJOR, MINOR, TINY, RELEASE_CANDIDATE].compact.join('_')}".upcase.gsub(/\.|-/, '_') |
| toggle raw diff |
--- a/rspec/lib/spec/version.rb
+++ b/rspec/lib/spec/version.rb
@@ -6,7 +6,7 @@ module Spec
TINY = 3
RELEASE_CANDIDATE = nil
- BUILD_TIME_UTC = 20080216153110
+ BUILD_TIME_UTC = 20080218031844
STRING = [MAJOR, MINOR, TINY].join('.')
TAG = "REL_#{[MAJOR, MINOR, TINY, RELEASE_CANDIDATE].compact.join('_')}".upcase.gsub(/\.|-/, '_')
|
| |   |
| 3 | 3 | describe <%= class_name %>Helper do |
| 4 | 4 | |
| 5 | 5 | #Delete this example and add some real ones or delete this file |
| 6 | | it "should include the <%= class_name %>Helper" do |
| 7 | | included_modules = (class << self; self; end).send :included_modules |
| 6 | it "should be included in the object returned by #helper" do |
| 7 | included_modules = (class << helper; self; end).send :included_modules |
| 8 | 8 | included_modules.should include(<%= class_name %>Helper) |
| 9 | 9 | end |
| 10 | | |
| 10 | |
| 11 | 11 | end |
| toggle raw diff |
--- a/rspec_on_rails/generators/rspec_controller/templates/helper_spec.rb
+++ b/rspec_on_rails/generators/rspec_controller/templates/helper_spec.rb
@@ -3,9 +3,9 @@ require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_hel
describe <%= class_name %>Helper do
#Delete this example and add some real ones or delete this file
- it "should include the <%= class_name %>Helper" do
- included_modules = (class << self; self; end).send :included_modules
+ it "should be included in the object returned by #helper" do
+ included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(<%= class_name %>Helper)
end
-
+
end |
| |   |
| 3 | 3 | describe <%= controller_class_name %>Helper do |
| 4 | 4 | |
| 5 | 5 | #Delete this example and add some real ones or delete this file |
| 6 | | it "should include the <%= class_name %>Helper" do |
| 7 | | included_modules = (class << self; self; end).send :included_modules |
| 6 | it "should be included in the object returned by #helper" do |
| 7 | included_modules = (class << helper; self; end).send :included_modules |
| 8 | 8 | included_modules.should include(<%= controller_class_name %>Helper) |
| 9 | 9 | end |
| 10 | 10 | |
| toggle raw diff |
--- a/rspec_on_rails/generators/rspec_scaffold/templates/helper_spec.rb
+++ b/rspec_on_rails/generators/rspec_scaffold/templates/helper_spec.rb
@@ -3,8 +3,8 @@ require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_hel
describe <%= controller_class_name %>Helper do
#Delete this example and add some real ones or delete this file
- it "should include the <%= class_name %>Helper" do
- included_modules = (class << self; self; end).send :included_modules
+ it "should be included in the object returned by #helper" do
+ included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(<%= controller_class_name %>Helper)
end
|
| |   |
| 26 | 26 | # end |
| 27 | 27 | # end |
| 28 | 28 | class HelperExampleGroup < FunctionalExampleGroup |
| 29 | class HelperObject < ActionView::Base |
| 30 | def protect_against_forgery? |
| 31 | false |
| 32 | end |
| 33 | end |
| 34 | |
| 29 | 35 | class << self |
| 30 | 36 | # The helper name.... |
| 31 | 37 | def helper_name(name=nil) |
| 32 | | send :include, "#{name}_helper".camelize.constantize |
| 38 | @helper_being_described = "#{name}_helper".camelize.constantize |
| 39 | send :include, @helper_being_described |
| 40 | end |
| 41 | |
| 42 | def helper |
| 43 | @helper_object ||= returning HelperObject.new do |helper_object| |
| 44 | if @helper_being_described.nil? |
| 45 | if described_type.class == Module |
| 46 | helper_object.extend described_type |
| 47 | end |
| 48 | else |
| 49 | helper_object.extend @helper_being_described |
| 50 | end |
| 51 | end |
| 33 | 52 | end |
| 53 | |
| 54 | end |
| 55 | |
| 56 | def helper |
| 57 | self.class.helper |
| 34 | 58 | end |
| 35 | 59 | |
| 36 | | # Reverse the load order so that custom helpers which |
| 37 | | # are defined last are also loaded last. |
| 60 | # Reverse the load order so that custom helpers which are defined last |
| 61 | # are also loaded last. |
| 38 | 62 | ActionView::Base.included_modules.reverse.each do |mod| |
| 39 | 63 | include mod if mod.parents.include?(ActionView::Helpers) |
| 40 | 64 | end |
| … | … | |
| 82 | 82 | end |
| 83 | 83 | |
| 84 | 84 | def eval_erb(text) |
| 85 | | ERB.new(text).result(binding) |
| 85 | helper.instance_eval do |
| 86 | ERB.new(text).result(binding) |
| 87 | end |
| 86 | 88 | end |
| 87 | 89 | |
| 88 | | |
| 89 | 90 | # TODO: BT - Helper Examples should proxy method_missing to a Rails View instance. |
| 90 | 91 | # When that is done, remove this method |
| 91 | 92 | def protect_against_forgery? |
| … | … | |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | 96 | Spec::Example::ExampleGroupFactory.register(:helper, self) |
| 97 | |
| 98 | protected |
| 99 | def _assigns_hash_proxy |
| 100 | @_assigns_hash_proxy ||= AssignsHashProxy.new helper |
| 101 | end |
| 102 | |
| 97 | 103 | end |
| 98 | 104 | |
| 99 | 105 | class HelperBehaviourController < ApplicationController #:nodoc: |
| toggle raw diff |
--- a/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb
+++ b/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb
@@ -26,15 +26,39 @@ module Spec
# end
# end
class HelperExampleGroup < FunctionalExampleGroup
+ class HelperObject < ActionView::Base
+ def protect_against_forgery?
+ false
+ end
+ end
+
class << self
# The helper name....
def helper_name(name=nil)
- send :include, "#{name}_helper".camelize.constantize
+ @helper_being_described = "#{name}_helper".camelize.constantize
+ send :include, @helper_being_described
+ end
+
+ def helper
+ @helper_object ||= returning HelperObject.new do |helper_object|
+ if @helper_being_described.nil?
+ if described_type.class == Module
+ helper_object.extend described_type
+ end
+ else
+ helper_object.extend @helper_being_described
+ end
+ end
end
+
+ end
+
+ def helper
+ self.class.helper
end
- # Reverse the load order so that custom helpers which
- # are defined last are also loaded last.
+ # Reverse the load order so that custom helpers which are defined last
+ # are also loaded last.
ActionView::Base.included_modules.reverse.each do |mod|
include mod if mod.parents.include?(ActionView::Helpers)
end
@@ -58,10 +82,11 @@ module Spec
end
def eval_erb(text)
- ERB.new(text).result(binding)
+ helper.instance_eval do
+ ERB.new(text).result(binding)
+ end
end
-
# TODO: BT - Helper Examples should proxy method_missing to a Rails View instance.
# When that is done, remove this method
def protect_against_forgery?
@@ -69,6 +94,12 @@ module Spec
end
Spec::Example::ExampleGroupFactory.register(:helper, self)
+
+ protected
+ def _assigns_hash_proxy
+ @_assigns_hash_proxy ||= AssignsHashProxy.new helper
+ end
+
end
class HelperBehaviourController < ApplicationController #:nodoc: |
| |   |
| 0 | | ## |
| 1 | | # A wrapper that allows instance variables to be manipulated using +[]+ and |
| 2 | | # +[]=+ |
| 3 | | |
| 4 | | module Spec |
| 5 | | module Rails |
| 6 | | module Example |
| 7 | | class IvarProxy #:nodoc: |
| 8 | | |
| 9 | | ## |
| 10 | | # Wraps +object+ allowing its instance variables to be manipulated. |
| 11 | | |
| 12 | | def initialize(object) |
| 13 | | @object = object |
| 14 | | end |
| 15 | | |
| 16 | | ## |
| 17 | | # Retrieves +ivar+ from the wrapped object. |
| 18 | | |
| 19 | | def [](ivar) |
| 20 | | get_variable "@#{ivar}" |
| 21 | | end |
| 22 | | |
| 23 | | ## |
| 24 | | # Sets +ivar+ to +val+ on the wrapped object. |
| 25 | | |
| 26 | | def []=(ivar, val) |
| 27 | | set_variable "@#{ivar}", val |
| 28 | | end |
| 29 | | |
| 30 | | def each |
| 31 | | @object.instance_variables.each do |variable_full_name| |
| 32 | | variable_name = variable_full_name[1...variable_full_name.length] |
| 33 | | yield variable_name, get_variable(variable_full_name) |
| 34 | | end |
| 35 | | end |
| 36 | | |
| 37 | | def delete(key) |
| 38 | | var_name = "@#{key}" |
| 39 | | if @object.instance_variables.include?(var_name) |
| 40 | | @object.send(:remove_instance_variable, var_name) |
| 41 | | else |
| 42 | | return nil |
| 43 | | end |
| 44 | | end |
| 45 | | |
| 46 | | def has_key?(key) |
| 47 | | @object.instance_variables.include?("@#{key}") |
| 48 | | end |
| 49 | | |
| 50 | | protected |
| 51 | | def get_variable(name) |
| 52 | | @object.instance_variable_get name |
| 53 | | end |
| 54 | | |
| 55 | | def set_variable(name, value) |
| 56 | | @object.instance_variable_set name, value |
| 57 | | end |
| 58 | | end |
| 59 | | end |
| 60 | | end |
| 61 | | end |
| toggle raw diff |
--- a/rspec_on_rails/lib/spec/rails/example/ivar_proxy.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-##
-# A wrapper that allows instance variables to be manipulated using +[]+ and
-# +[]=+
-
-module Spec
- module Rails
- module Example
- class IvarProxy #:nodoc:
-
- ##
- # Wraps +object+ allowing its instance variables to be manipulated.
-
- def initialize(object)
- @object = object
- end
-
- ##
- # Retrieves +ivar+ from the wrapped object.
-
- def [](ivar)
- get_variable "@#{ivar}"
- end
-
- ##
- # Sets +ivar+ to +val+ on the wrapped object.
-
- def []=(ivar, val)
- set_variable "@#{ivar}", val
- end
-
- def each
- @object.instance_variables.each do |variable_full_name|
- variable_name = variable_full_name[1...variable_full_name.length]
- yield variable_name, get_variable(variable_full_name)
- end
- end
-
- def delete(key)
- var_name = "@#{key}"
- if @object.instance_variables.include?(var_name)
- @object.send(:remove_instance_variable, var_name)
- else
- return nil
- end
- end
-
- def has_key?(key)
- @object.instance_variables.include?("@#{key}")
- end
-
- protected
- def get_variable(name)
- @object.instance_variable_get name
- end
-
- def set_variable(name, value)
- @object.instance_variable_set name, value
- end
- end
- end
- end
-end |
| |   |
| 1 | 1 | require File.dirname(__FILE__) + '/../../spec_helper' |
| 2 | 2 | |
| 3 | | describe "An AssignsHashProxy" do |
| 3 | describe "AssignsHashProxy" do |
| 4 | 4 | before(:each) do |
| 5 | 5 | @object = Object.new |
| 6 | 6 | @assigns = Hash.new |
| … | … | |
| 52 | 52 | @proxy.has_key?('foo').should == true |
| 53 | 53 | @proxy.has_key?('bar').should == false |
| 54 | 54 | end |
| 55 | |
| 56 | it "should sets an instance var" do |
| 57 | @proxy['foo'] = 'bar' |
| 58 | @object.instance_eval { @foo }.should == 'bar' |
| 59 | end |
| 55 | 60 | end |
| toggle raw diff |
--- a/rspec_on_rails/spec/rails/example/assigns_hash_proxy_spec.rb
+++ b/rspec_on_rails/spec/rails/example/assigns_hash_proxy_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../../spec_helper'
-describe "An AssignsHashProxy" do
+describe "AssignsHashProxy" do
before(:each) do
@object = Object.new
@assigns = Hash.new
@@ -52,4 +52,9 @@ describe "An AssignsHashProxy" do
@proxy.has_key?('foo').should == true
@proxy.has_key?('bar').should == false
end
+
+ it "should sets an instance var" do
+ @proxy['foo'] = 'bar'
+ @object.instance_eval { @foo }.should == 'bar'
+ end
end |
| |   |
| 4 | 4 | describe ExplicitHelper, :type => :helper do |
| 5 | 5 | it "should not require naming the helper if describe is passed a type" do |
| 6 | 6 | method_in_explicit_helper.should match(/text from a method/) |
| 7 | helper.method_in_explicit_helper.should match(/text from a method/) |
| 7 | 8 | end |
| 8 | 9 | end |
| 9 | 10 | |
| … | … | |
| 14 | 14 | describe HelperExampleGroup, :type => :helper do |
| 15 | 15 | helper_name :explicit |
| 16 | 16 | |
| 17 | | it "should have direct access to methods defined in helpers" do |
| 17 | it "DEPRECATED should have direct access to methods defined in helpers" do |
| 18 | 18 | method_in_explicit_helper.should =~ /text from a method/ |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | it "should expose the helper with the #helper method" do |
| 22 | helper.method_in_explicit_helper.should =~ /text from a method/ |
| 23 | end |
| 24 | |
| 21 | 25 | it "should have access to named routes" do |
| 22 | 26 | rspec_on_rails_specs_url.should == "http://test.host/rspec_on_rails_specs" |
| 23 | 27 | rspec_on_rails_specs_path.should == "/rspec_on_rails_specs" |
| 24 | 28 | end |
| 25 | 29 | |
| 26 | 30 | it "should fail if the helper method deson't exist" do |
| 27 | | lambda { non_existant_helper_method }.should raise_error(NameError) |
| 31 | lambda { non_existent_helper_method }.should raise_error(NameError) |
| 32 | lambda { helper.non_existent_helper_method }.should raise_error(NameError) |
| 28 | 33 | end |
| 29 | 34 | end |
| 30 | 35 | |
| … | … | |
| 56 | 56 | lachie.class.should == Person |
| 57 | 57 | end |
| 58 | 58 | end |
| 59 | |
| 60 | describe "methods from standard helpers", :type => :helper do |
| 61 | helper_name :explicit |
| 62 | it "should be exposed to the helper" do |
| 63 | helper.link_to("Foo","http://bar").should have_tag("a") |
| 64 | end |
| 65 | end |
| 59 | 66 | |
| 60 | 67 | describe HelperExampleGroup, "included modules", :type => :helper do |
| 61 | 68 | helpers = [ |
| … | … | |
| 87 | 87 | helpers << ActionView::Helpers::PaginationHelper rescue nil #removed for 2.0 |
| 88 | 88 | helpers << ActionView::Helpers::JavaScriptMacrosHelper rescue nil #removed for 2.0 |
| 89 | 89 | helpers.each do |helper_module| |
| 90 | | it "should include #{helper_module}" do |
| 91 | | self.class.ancestors.should include(helper_module) |
| 92 | | end |
| 90 | # it "should include #{helper_module}" do |
| 91 | # self.class.ancestors.should include(helper_module) |
| 92 | # helper.class.ancestors.should include(helper_module) |
| 93 | # end |
| 93 | 94 | end |
| 94 | 95 | end |
| 95 | 96 | |
| … | … | |
| 99 | 99 | describe HelperExampleGroup, "#protect_against_forgery?", :type => :helper do |
| 100 | 100 | it "should return false" do |
| 101 | 101 | protect_against_forgery?.should be_false |
| 102 | helper.protect_against_forgery?.should be_false |
| 102 | 103 | end |
| 103 | 104 | end |
| 104 | 105 | end |
| toggle raw diff |
--- a/rspec_on_rails/spec/rails/example/helper_spec_spec.rb
+++ b/rspec_on_rails/spec/rails/example/helper_spec_spec.rb
@@ -4,6 +4,7 @@ Spec::Runner.configuration.global_fixtures = :people
describe ExplicitHelper, :type => :helper do
it "should not require naming the helper if describe is passed a type" do
method_in_explicit_helper.should match(/text from a method/)
+ helper.method_in_explicit_helper.should match(/text from a method/)
end
end
@@ -13,17 +14,22 @@ module Spec
describe HelperExampleGroup, :type => :helper do
helper_name :explicit
- it "should have direct access to methods defined in helpers" do
+ it "DEPRECATED should have direct access to methods defined in helpers" do
method_in_explicit_helper.should =~ /text from a method/
end
+ it "should expose the helper with the #helper method" do
+ helper.method_in_explicit_helper.should =~ /text from a method/
+ end
+
it "should have access to named routes" do
rspec_on_rails_specs_url.should == "http://test.host/rspec_on_rails_specs"
rspec_on_rails_specs_path.should == "/rspec_on_rails_specs"
end
it "should fail if the helper method deson't exist" do
- lambda { non_existant_helper_method }.should raise_error(NameError)
+ lambda { non_existent_helper_method }.should raise_error(NameError)
+ lambda { helper.non_existent_helper_method }.should raise_error(NameError)
end
end
@@ -50,6 +56,13 @@ module Spec
lachie.class.should == Person
end
end
+
+ describe "methods from standard helpers", :type => :helper do
+ helper_name :explicit
+ it "should be exposed to the helper" do
+ helper.link_to("Foo","http://bar").should have_tag("a")
+ end
+ end
describe HelperExampleGroup, "included modules", :type => :helper do
helpers = [
@@ -74,9 +87,10 @@ module Spec
helpers << ActionView::Helpers::PaginationHelper rescue nil #removed for 2.0
helpers << ActionView::Helpers::JavaScriptMacrosHelper rescue nil #removed for 2.0
helpers.each do |helper_module|
- it "should include #{helper_module}" do
- self.class.ancestors.should include(helper_module)
- end
+ # it "should include #{helper_module}" do
+ # self.class.ancestors.should include(helper_module)
+ # helper.class.ancestors.should include(helper_module)
+ # end
end
end
@@ -85,6 +99,7 @@ module Spec
describe HelperExampleGroup, "#protect_against_forgery?", :type => :helper do
it "should return false" do
protect_against_forgery?.should be_false
+ helper.protect_against_forgery?.should be_false
end
end
end |
| |   |
| 0 | | require File.dirname(__FILE__) + '/../../spec_helper' |
| 1 | | |
| 2 | | describe "IvarProxy setup", :shared => true do |
| 3 | | before do |
| 4 | | @object = Object.new |
| 5 | | @proxy = Spec::Rails::Example::IvarProxy.new(@object) |
| 6 | | end |
| 7 | | end |
| 8 | | |
| 9 | | describe "IvarProxy" do |
| 10 | | it_should_behave_like "IvarProxy setup" |
| 11 | | |
| 12 | | it "has [] accessor" do |
| 13 | | @proxy['foo'] = 'bar' |
| 14 | | @object.instance_variable_get(:@foo).should == 'bar' |
| 15 | | @proxy['foo'].should == 'bar' |
| 16 | | end |
| 17 | | |
| 18 | | it "iterates through each element like a Hash" do |
| 19 | | values = { |
| 20 | | 'foo' => 1, |
| 21 | | 'bar' => 2, |
| 22 | | 'baz' => 3 |
| 23 | | } |
| 24 | | @proxy['foo'] = values['foo'] |
| 25 | | @proxy['bar'] = values['bar'] |
| 26 | | @proxy['baz'] = values['baz'] |
| 27 | | |
| 28 | | @proxy.each do |key, value| |
| 29 | | key.should == key |
| 30 | | value.should == values[key] |
| 31 | | end |
| 32 | | end |
| 33 | | |
| 34 | | it "detects the presence of a key" do |
| 35 | | @proxy['foo'] = 'bar' |
| 36 | | @proxy.has_key?('foo').should == true |
| 37 | | @proxy.has_key?('bar').should == false |
| 38 | | end |
| 39 | | end |
| 40 | | |
| 41 | | describe "IvarProxy", "#delete" do |
| 42 | | it_should_behave_like "IvarProxy setup" |
| 43 | | |
| 44 | | it "deletes the element with key" do |
| 45 | | @proxy['foo'] = 'bar' |
| 46 | | @proxy.delete('foo').should == 'bar' |
| 47 | | @proxy['foo'].should be_nil |
| 48 | | end |
| 49 | | |
| 50 | | it "deletes nil instance variables" do |
| 51 | | @proxy['foo'] = nil |
| 52 | | @object.instance_variables.should include("@foo") |
| 53 | | @proxy.delete('foo').should == nil |
| 54 | | @proxy['foo'].should be_nil |
| 55 | | @object.instance_variables.should_not include("@foo") |
| 56 | | end |
| 57 | | |
| 58 | | it "returns nil when key does not exist" do |
| 59 | | @proxy['foo'].should be_nil |
| 60 | | @proxy.delete('foo').should == nil |
| 61 | | @proxy['foo'].should be_nil |
| 62 | | end |
| 63 | | end |
| toggle raw diff |
--- a/rspec_on_rails/spec/rails/example/ivar_proxy_spec.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-require File.dirname(__FILE__) + '/../../spec_helper'
-
-describe "IvarProxy setup", :shared => true do
- before do
- @object = Object.new
- @proxy = Spec::Rails::Example::IvarProxy.new(@object)
- end
-end
-
-describe "IvarProxy" do
- it_should_behave_like "IvarProxy setup"
-
- it "has [] accessor" do
- @proxy['foo'] = 'bar'
- @object.instance_variable_get(:@foo).should == 'bar'
- @proxy['foo'].should == 'bar'
- end
-
- it "iterates through each element like a Hash" do
- values = {
- 'foo' => 1,
- 'bar' => 2,
- 'baz' => 3
- }
- @proxy['foo'] = values['foo']
- @proxy['bar'] = values['bar']
- @proxy['baz'] = values['baz']
-
- @proxy.each do |key, value|
- key.should == key
- value.should == values[key]
- end
- end
-
- it "detects the presence of a key" do
- @proxy['foo'] = 'bar'
- @proxy.has_key?('foo').should == true
- @proxy.has_key?('bar').should == false
- end
-end
-
-describe "IvarProxy", "#delete" do
- it_should_behave_like "IvarProxy setup"
-
- it "deletes the element with key" do
- @proxy['foo'] = 'bar'
- @proxy.delete('foo').should == 'bar'
- @proxy['foo'].should be_nil
- end
-
- it "deletes nil instance variables" do
- @proxy['foo'] = nil
- @object.instance_variables.should include("@foo")
- @proxy.delete('foo').should == nil
- @proxy['foo'].should be_nil
- @object.instance_variables.should_not include("@foo")
- end
-
- it "returns nil when key does not exist" do
- @proxy['foo'].should be_nil
- @proxy.delete('foo').should == nil
- @proxy['foo'].should be_nil
- end
-end |