On Sep 3, 2008, at 10:53 AM, Scott Taylor wrote:


On Sep 3, 2008, at 10:40 AM, MaurĂ­cio Linhares wrote:

You don't need to set the instance variable, you can stub the find call:


Or you could also stub the call to User::find.

Also - you don't *WANT* to set the instance variable - it's an encapsulation violation - there is nothing in the public interface about it. Remember, these are not only specs, but *examples*. Would you want someone writing production code modeled off of that spec?

Scott

Often I'll encapsulate important instance variables (especially ones assigned in before_filters) in a method, like current_user, current_project, etc.

    def current_project
        @project
    end

Then these are easily stubbed in specs.

        controller.stub!(:current_project).and_return(project)

Further, I usually mock the entire before_filter methods

        before_filter :find_project

with a shared method like

def mock_find_project
  project = @project || mock_project
  controller.stub!(:find_project).and_return(project)
  controller.stub!(:current_project).and_return(project)
  project
end

Lastly, I maintain the find_project method in application.rb so it can be shared among controllers, and tested separately. (either with shared behaviors, or in my case i've rigged up an application_controller_spec that lets me test methods in application.rb independent of a specific controller).

--linoj
        

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to