1

I'm writing RSpec integration tests as I convert my spaghetti code to use accepts_nested_attributes_for. I have a snippet like this:

# file: spec/requests/wizard_spec.rb
describe 'POST /wizard with address' do
  before(:each) do
    @premise_attributes = {
      "address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA", 
    }
  end
  it 'should succeed' do
    post :create, "wizard" => { "premise_attributes" => @premise_attributes }
    response.status.should be(200)
  end
end

Of course, this fails with:

 Failure/Error: post :create, "wizard" => { "premise_attributes" => @premise_attributes }
 ArgumentError:
   bad argument(expected URI object or URI string)

Is there a method that converts the nested attributes hashes into a POST-able format?

(Related but less important: where is the post method documented or defined? I'd like to see what it really accepts as arguments.)

2 Answers 2

1

Instead post :create try use post "/wizard" or nest your specs inside describe WizardController do; end block. Generally you can use method :action syntax only if you're inside describe block for the given controller.

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

Comments

0

I found this while trying to fix my issue with trying to test put. My post method works though so maybe I can help you out if you still need it. I think your issue is that you're trying to update your attributes as if it was a scalar type variable, but nested attributes are really like an array. Rails generally names them "0", "1", etc., but I'm not sure it matters what the names are as long as they're unique. Give this a try:

@premise_attributes = {
  "0" => {"address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA"}
}

(By the way, the problem I'm having is that my update specs are failing because it says something like my address is not unique to borrow your example.)

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.