[rspec-users] Specs for ApplicationController, where to put them?

Wincent Colaiuta win at wincent.com
Thu May 31 03:25:49 EDT 2007

El 31/5/2007, a las 7:27, Wincent Colaiuta escribió:

> El 31/5/2007, a las 0:51, David Chelimsky escribió:
>> On 5/30/07, Wincent Colaiuta <win at wincent.com> wrote:
>>> El 30/5/2007, a las 23:11, David Chelimsky escribió:
>>>> I think you're spot on about all of this. I'm thinking that a  
>>>> simple
>>>> solution would be that each time a shared behaviour is
>>>> registered, the
>>>> location of its definition can be registered as well. Then we can
>>>> ignore the ones defined in the same place when a file gets  
>>>> reloaded,
>>>> but still complain when another one is defined with the same name
>>>> from
>>>> a different location.
>>>> WDYT?
>>> Would be great -- it would basically capture the intent of your
>>> current implementation -- but how would you get the location of its
>>> definition?
>> By inspecting caller. We do this already in building up the  
>> backtrace.
>> I'll pursue this soon.
> Ah, of course... I didn't even realize that Kernel#caller was in the
> Ruby core... That will definitely work then.

The following seems to work. Some things to note:

- the code in the trunk (as at r2061) allows "found_behaviour" to be  
nil and adds it to the shared_behaviours array, which seems wrong;  
this patch fixes that

- the patch uses the already-present :spec_path info (extracted using  
Kernel#caller in the "describe" method) as this seems the cleanest  
way of geting location info

- I've added some specs to confirm that things work... note that  
because the :spec_path info gets put in place only when using  
"describe" a couple of the specs have to directly use the "describe"  
method rather than the "make_shared_behaviour" helper method

- fixed a typo in one of the examples ("when adding a the same")

Is this something like what you had in mind?


Index: rspec/lib/spec/dsl/behaviour.rb
--- rspec/lib/spec/dsl/behaviour.rb     (revision 2061)
+++ rspec/lib/spec/dsl/behaviour.rb     (working copy)
@@ -7,6 +7,7 @@
        class << self
          def add_shared_behaviour(behaviour)
            return if behaviour.equal?(found_behaviour =  
+          return if found_behaviour and behaviour.description 
[:spec_path] == found_behaviour.description[:spec_path]
            raise ArgumentError.new("Shared Behaviour '# 
{behaviour.description}' already exists") if found_behaviour
            shared_behaviours << behaviour
Index: rspec/spec/spec/dsl/shared_behaviour_spec.rb
--- rspec/spec/spec/dsl/shared_behaviour_spec.rb        (revision 2061)
+++ rspec/spec/spec/dsl/shared_behaviour_spec.rb        (working copy)
@@ -74,16 +74,23 @@
        it "should complain when adding a second shared behaviour  
with the same description" do
-        make_shared_behaviour("shared behaviour") {}
-        lambda { make_shared_behaviour("shared behaviour")  
{} }.should raise_error(ArgumentError)
+        describe "shared behaviour", :shared => true do
+        end
+        lambda { describe "shared behaviour", :shared => true do
+          end }.should raise_error(ArgumentError)
-      it "should NOT complain when adding a the same shared  
behaviour again (i.e. file gets reloaded)" do
-        behaviour = behaviour_class.new("shared behaviour") {}
-        behaviour_class.add_shared_behaviour(behaviour)
-        behaviour_class.add_shared_behaviour(behaviour)
+      it "should NOT complain when adding the same shared behaviour  
instance again" do
+        shared_behaviour = make_shared_behaviour("shared behaviour") {}
+        behaviour_class.add_shared_behaviour(shared_behaviour)
+        behaviour_class.add_shared_behaviour(shared_behaviour)
+      it "should NOT complain when adding the same shared behaviour  
again (i.e. file gets reloaded)" do
+        lambda { 2.times { describe "shared behaviour", :shared =>  
true do
+          end } }.should_not raise_error(ArgumentError)
+      end
        it "should add examples to current behaviour when calling  
it_should_behave_like" do
          shared_behaviour = make_shared_behaviour("shared  
behaviour") {}
          shared_behaviour.it("shared example") {}

More information about the rspec-users mailing list