2

I have routes set up that look like:

match '/things/:thing_id' => "descriptions#index", :as => :thing
resources :things, :except => [:show] do 
  ...
  resources :descriptions, :only => [:create, :index]
end

How would I test the :create method for the nested descriptions?

So far i've got

context "with user signed in" do

  before(:each) do
    user = Factory.create(:user, :name => "Bob")
    controller.stub!(:authenticate_user!).and_return(true)
    controller.stub!(:current_user).and_return(user)
  end

  describe "PUT create" do

    before(:each) do
      @thing = Factory.create(:thing)
      params = {"text" => "Happy Text"}

      post thing_descriptions_path(@thing.id), params  #Doesn't work
      post :create, params                             #Doesn't work

    end
  end
end
1
  • What is the name of the spec in which you have this code? is it in the things_spec or the descriptions_spec? it should be in the latter. Commented Nov 8, 2011 at 12:50

1 Answer 1

3

The following should work:

describe "PUT create" do

  before(:each) do
    @thing = Factory(:thing)
    attrs = FactoryGirl.attributes_for(:description, :thing_id => @thing)

    post :create, :thing_id => @thing, :description => attrs
  end
end

To create a nested resource you need to tell rspec the parent id in the post create so that it can insert it into the route.

You also need to create your :descriptions factory with the relationship to :thing built in, and also pass the thing_id into the :description attribute creation, this is to make sure that Factory Girl doesn't go and create a new :thing when it creates the attributes for :description, while this wouldn't cause the test to fail it would slow it down as you would end up creating two instances of :thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.