[rspec-users] Controller specs not shareable?

David Chelimsky dchelimsky at gmail.com
Wed Jun 27 07:31:16 EDT 2007


On 6/27/07, Ashley Moran <work at ashleymoran.me.uk> wrote:
> Hi
>
> I just wondered if there was a reason why Rails controller specs are
> not shareable?  eg
>
>    describe "All XHR POSTs to /gap/calculate_quote", :shared => true do
>      controller_name :gap
>
>      # ...
> end
>
> describe "XHR POST /gap/calculate_quote with valid details" do
>    it_should_behave_like "All XHR POSTs to /gap/calculate_quote"
>
>    # ...
> end

Try this instead:

describe "All XHR POSTs to /gap/calculate_quote", :shared => true do
    # ...
end

describe GapController, "XHR POST /gap/calculate_quote with valid details" do
  it_should_behave_like "All XHR POSTs to /gap/calculate_quote"

  # ...
end

The idea of shared behaviours is that multiple things that are being
described have similar behaviour. The specific things, however, should
never be created in the shared behaviour - they are the responsibility
of the behaviours that should_behave_like them.

Make sense?


>
> blows up with the following error:
>
> ##########
> /Users/ashleymoran/Documents/Development/YourMoney/trunk/src/vendor/
> plugins/rspec/lib/spec/dsl/behaviour_eval.rb:137:in `method_missing':
> undefined method `controller_name' for # (NoMethodError) from ./spec/
> controllers/gap_controller_spec.rb:165 from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/dsl/behaviour.rb:54:in `class_eval' from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/dsl/behaviour.rb:54:in `eval_behaviour' from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/dsl/behaviour.rb:31:in `original_initialize' from /Users/
> ashleymoran/Documents/Development/YourMoney/trunk/src/spec/
> spec_helper_additions.rb:25:in `initialize' from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/dsl/behaviour_factory.rb:36:in `new' from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/dsl/behaviour_factory.rb:36:in `create' from /Users/ashleymoran/
> Documents/Development/YourMoney/trunk/src/vendor/plugins/rspec/lib/
> spec/runner/extensions/kernel.rb:24:in `describe' ... 8 levels...
> from /Users/ashleymoran/Library/Application Support/TextMate/Pristine
> Copy/Bundles/RSpec.tmbundle/Support/lib/spec_mate.rb:46:in `chdir'
> from /Users/ashleymoran/Library/Application Support/TextMate/Pristine
> Copy/Bundles/RSpec.tmbundle/Support/lib/spec_mate.rb:46:in `run'
> from /Users/ashleymoran/Library/Application Support/TextMate/Pristine
> Copy/Bundles/RSpec.tmbundle/Support/lib/spec_mate.rb:25:in `run_file'
> from /tmp/temp_textmate.Ft0uGO:4
> ##########
>
> I've had a quick look through the source and can sorta see why it
> doesn't work.  Is there an easy way to fix this or any plans to add
> it in?  The controller specs are where virtually all my duplication is.
>
> Ashley
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list