Patrick J. Collins
2012-Jun-19 05:16 UTC
[rspec-users] shared examples with controllers + included modules?
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
David Chelimsky
2012-Jun-19 10:16 UTC
[rspec-users] shared examples with controllers + included modules?
On Tue, Jun 19, 2012 at 12:16 AM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:> Hi everyone, > > I ran into a little problem this evening and couldn''t quite figure out > how to solve it... ><snip/>> So, then I had a controller test like: > > describe FooController do > ?it_behaves_like "omg", subject > end > > ... > > shared_example "omg" do |obj|<snip/>> > ?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?Assuming this is a controller spec, you can reference the controller via the `controller` method (courtesy of ActionController::TestCase::Behavior [1][2][3]): shared_examples "omg" do it "gets something" do Something.stubs(:new).returns "lol" get :show controller.something.should == "lol" end end describe FooController do it_behaves_like "omg" end HTH, David [1] https://github.com/rspec/rspec-rails/blob/9abdc0a588a78d3cd50bb58ce719ce72f9af34e1/lib/rspec/rails/example/controller_example_group.rb#L9 [2] https://github.com/rspec/rspec-rails/blob/9abdc0a588a78d3cd50bb58ce719ce72f9af34e1/lib/rspec/rails/example/controller_example_group.rb#L77 [3] https://github.com/rails/rails/blob/c1b1956a15d3d38d0a4504e168bb69638d71e536/actionpack/lib/action_controller/test_case.rb#L508