[rspec-users] shared examples with controllers + included modules?

Patrick J. Collins patrick at collinatorstudios.com
Tue Jun 19 05:16:44 UTC 2012


Hi everyone,

I ran into a little problem this evening and couldn't quite figure out
how to solve it...

So I have this sort of thing:

class FooController < ApplicationController
  include Omg
end

module Omg

  def self.included(base)
    base.before_filter :set_something, :only => :show
    base.class_eval do
      attr_reader :something
    end
  end

  def set_something
    @something ||= Something.new(current_user)
  end

end

....

So, then I had a controller test like:

describe FooController do
  it_behaves_like "omg", subject
end

...

shared_example "omg" do |obj|
  it "sets something" do
    Something.expects(:new).once
    get :show
  end

  it "gets something" do
    Something.stubs(:new).returns "lol"
    get :show
    obj.something.should == "lol"
  end
end

.........

So this failed, and I saw-- ok-- subject apparently isn't a real
instance of the controller outside of an "it" block......  It's a proc
of some kind, and I didn't know what to do with it-- so I changed my
test code slightly:

describe FooController do
  it_behaves_like "omg", FooController.new
end

... and I got a failure, and upon inspecting, I found that in side the
context of the shared examples, the "obj" variable was not the same
controller instance as the one performing the get :show...  So
obj.something never equalled "lol", because it never got its setter
method called..

So I said to mself: "????????"

and thought I'd ask here:

How can I refernence the real subject link this inside a shared example?

Patrick J. Collins
http://collinatorstudios.com



More information about the rspec-users mailing list