> > My controller does something like this for create
> > @object = Object.new(params[:object])
> > @object.created_by = current_user
> > if @object.save... yadda yadda yadda...
>
> > I have my model spec testing for the presence of created_by but I feel
> > compelled to test that my controller is correctly setting current_user
> > on its appropriate actions. Does this seem reasonable? If so, what's
> > the proper way to test it?
>
> Seems reasonable to me. You could test with either an interaction based 
> approach (use mocks and stubs to confirm that @object receives the 
> "created_by" message with the expected param) or a state based approach (hit 
> the controller action and then inspect the system state afterwards to confirm 
> that it is in the expected state).

What I was trying to do on my own felt a lot like what you're calling
an interaction based approach. I want to leave the behavior testing of
my app to cuke features and I'm thinking that if I'm writing a
controller spec, then it would probably be right to test the internals
of my controllers. (As an aside, I've only been using RSpec for ~3
weeks now and testing in general for maybe a month. All of this is
pretty new to me... I find myself quickly becoming 'opinionated' about
things so if any of this sounds like nonsense, a pointer in a good
direction would be happily accepted.)

Here's a stripped down excerpt from my controller spec. I know how
Order.should_receive(:method) works, but how do I translate that to an
instance variable? I've tried assigns(:order).should_receive to no
avail. Also, where do I put it? Above the post line won't work because
@order doesn't exist but after doesn't work either because my action
has already been run.

describe OrdersController, :type => :controller do
  context "when a user who is authenticated" do
    before(:each) do
      sign_in_user
    end
    context "requests POST /orders" do
      context "with valid parameters" do
        before(:each) do
          Order.stub(:new) { mock_order(:save => true) }
        end

        it "should explicitly set created_by" do
          post :create
        end
      end
    end
  end
end

I'm still reading the docs and will make some more attempts to get it
right. Thank you for your help.

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

Reply via email to