Commit b824e072d0085311fd318fcf8f3e461b037d05cf

Added #helper method to helper specs.
Added support for assigns in helper specs.
Removed redundant ivar_proxy (same as assigns_hash_proxy).

git-svn-id: http://rspec.rubyforge.org/svn/trunk@3314 410327ef-2207-0410-a325-f78bbcb22a5a

Commit diff

example_rails_app/spec/helpers/people_helper_spec.rb

 
22
33describe PeopleHelper do
44 it "should say hello" do
5 say_hello.should == "Hello"
5 helper.say_hello.should == "Hello"
66 end
77
88 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\" />"
2111 end
2212end
toggle raw diff

rspec/lib/spec/version.rb

 
66 TINY = 3
77 RELEASE_CANDIDATE = nil
88
9 BUILD_TIME_UTC = 20080216153110
9 BUILD_TIME_UTC = 20080218031844
1010
1111 STRING = [MAJOR, MINOR, TINY].join('.')
1212 TAG = "REL_#{[MAJOR, MINOR, TINY, RELEASE_CANDIDATE].compact.join('_')}".upcase.gsub(/\.|-/, '_')
toggle raw diff

rspec_on_rails/generators/rspec_controller/templates/helper_spec.rb

 
33describe <%= class_name %>Helper do
44
55 #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
88 included_modules.should include(<%= class_name %>Helper)
99 end
10
10
1111end
toggle raw diff

rspec_on_rails/generators/rspec_scaffold/templates/helper_spec.rb

 
33describe <%= controller_class_name %>Helper do
44
55 #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
88 included_modules.should include(<%= controller_class_name %>Helper)
99 end
1010
toggle raw diff

rspec_on_rails/lib/spec/rails/example.rb

 
11dir = File.dirname(__FILE__)
22
3require 'spec/rails/example/ivar_proxy'
43require 'spec/rails/example/assigns_hash_proxy'
54
65require "spec/rails/example/render_observer"
toggle raw diff

rspec_on_rails/lib/spec/rails/example/assigns_hash_proxy.rb

 
1717 end
1818
1919 def []=(ivar, val)
20 @object.instance_variable_set "@#{ivar}", val
2021 assigns[ivar.to_s] = val
2122 end
2223
toggle raw diff

rspec_on_rails/lib/spec/rails/example/controller_example_group.rb

 
150150 end
151151
152152 protected
153 def _controller_ivar_proxy
154 @controller_ivar_proxy ||= AssignsHashProxy.new @controller
153 def _assigns_hash_proxy
154 @_assigns_hash_proxy ||= AssignsHashProxy.new @controller
155155 end
156156
157157 private
toggle raw diff

rspec_on_rails/lib/spec/rails/example/functional_example_group.rb

 
4848 #++
4949 def assigns(key = nil)
5050 if key.nil?
51 @controller.assigns
52 _controller_ivar_proxy
51 _assigns_hash_proxy
5352 else
54 @controller.assigns[key]
55 _controller_ivar_proxy[key]
53 _assigns_hash_proxy[key]
5654 end
5755 end
58
59 protected
60 def _controller_ivar_proxy
61 @controller_ivar_proxy ||= IvarProxy.new @controller
62 end
6356 end
6457 end
6558 end
toggle raw diff

rspec_on_rails/lib/spec/rails/example/helper_example_group.rb

 
2626 # end
2727 # end
2828 class HelperExampleGroup < FunctionalExampleGroup
29 class HelperObject < ActionView::Base
30 def protect_against_forgery?
31 false
32 end
33 end
34
2935 class << self
3036 # The helper name....
3137 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
3352 end
53
54 end
55
56 def helper
57 self.class.helper
3458 end
3559
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.
3862 ActionView::Base.included_modules.reverse.each do |mod|
3963 include mod if mod.parents.include?(ActionView::Helpers)
4064 end
8282 end
8383
8484 def eval_erb(text)
85 ERB.new(text).result(binding)
85 helper.instance_eval do
86 ERB.new(text).result(binding)
87 end
8688 end
8789
88
8990 # TODO: BT - Helper Examples should proxy method_missing to a Rails View instance.
9091 # When that is done, remove this method
9192 def protect_against_forgery?
9494 end
9595
9696 Spec::Example::ExampleGroupFactory.register(:helper, self)
97
98 protected
99 def _assigns_hash_proxy
100 @_assigns_hash_proxy ||= AssignsHashProxy.new helper
101 end
102
97103 end
98104
99105 class HelperBehaviourController < ApplicationController #:nodoc:
toggle raw diff

rspec_on_rails/lib/spec/rails/example/ivar_proxy.rb

 
0##
1# A wrapper that allows instance variables to be manipulated using +[]+ and
2# +[]=+
3
4module 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
61end
toggle raw diff

rspec_on_rails/lib/spec/rails/example/view_example_group.rb

 
147147 end
148148
149149 Spec::Example::ExampleGroupFactory.register(:view, self)
150
151 protected
152 def _assigns_hash_proxy
153 @_assigns_hash_proxy ||= AssignsHashProxy.new @controller
154 end
150155 end
151156
152157 class ViewExampleGroupController < ApplicationController #:nodoc:
toggle raw diff

rspec_on_rails/lib/spec/rails/version.rb

 
11module Spec
22 module Rails
33 module VERSION #:nodoc:
4 BUILD_TIME_UTC = 20080216153110
4 BUILD_TIME_UTC = 20080218031844
55 end
66 end
77end
toggle raw diff

rspec_on_rails/spec/rails/example/assigns_hash_proxy_spec.rb

 
11require File.dirname(__FILE__) + '/../../spec_helper'
22
3describe "An AssignsHashProxy" do
3describe "AssignsHashProxy" do
44 before(:each) do
55 @object = Object.new
66 @assigns = Hash.new
5252 @proxy.has_key?('foo').should == true
5353 @proxy.has_key?('bar').should == false
5454 end
55
56 it "should sets an instance var" do
57 @proxy['foo'] = 'bar'
58 @object.instance_eval { @foo }.should == 'bar'
59 end
5560end
toggle raw diff

rspec_on_rails/spec/rails/example/helper_spec_spec.rb

 
44describe ExplicitHelper, :type => :helper do
55 it "should not require naming the helper if describe is passed a type" do
66 method_in_explicit_helper.should match(/text from a method/)
7 helper.method_in_explicit_helper.should match(/text from a method/)
78 end
89end
910
1414 describe HelperExampleGroup, :type => :helper do
1515 helper_name :explicit
1616
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
1818 method_in_explicit_helper.should =~ /text from a method/
1919 end
2020
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
2125 it "should have access to named routes" do
2226 rspec_on_rails_specs_url.should == "http://test.host/rspec_on_rails_specs"
2327 rspec_on_rails_specs_path.should == "/rspec_on_rails_specs"
2428 end
2529
2630 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)
2833 end
2934 end
3035
5656 lachie.class.should == Person
5757 end
5858 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
5966
6067 describe HelperExampleGroup, "included modules", :type => :helper do
6168 helpers = [
8787 helpers << ActionView::Helpers::PaginationHelper rescue nil #removed for 2.0
8888 helpers << ActionView::Helpers::JavaScriptMacrosHelper rescue nil #removed for 2.0
8989 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
9394 end
9495 end
9596
9999 describe HelperExampleGroup, "#protect_against_forgery?", :type => :helper do
100100 it "should return false" do
101101 protect_against_forgery?.should be_false
102 helper.protect_against_forgery?.should be_false
102103 end
103104 end
104105 end
toggle raw diff

rspec_on_rails/spec/rails/example/ivar_proxy_spec.rb

 
0require File.dirname(__FILE__) + '/../../spec_helper'
1
2describe "IvarProxy setup", :shared => true do
3 before do
4 @object = Object.new
5 @proxy = Spec::Rails::Example::IvarProxy.new(@object)
6 end
7end
8
9describe "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
39end
40
41describe "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
63end
toggle raw diff