2

I am a beginner in Rails and using old version 4.0 with Rspec. I want to test on the controller where my route is following.

/properties/:property_id/build

I can test /properties/1 by writing the following

get :show, id: properties
expect(response).to have_http_status(:success)

But not sure how can I write property's id number to build controller whose route is above. It means to show the method I have to put properties id and build but later on for update I have to put two parameters.

3 Answers 3

3

You can pass property_id with params like

get :build, params: { property_id: property.id } #You pass additional parameters with this.  

Here property_id is :property_id in /properties/:property_id/build

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

Comments

1

If its a get request then,

get :build, params: {id: properties, second_param: "something" }

(assuming properties is the Property object from your example and the second param is second_param)

3 Comments

There is one get request and one post request but params are in url too!
you can put all the params inside params key to test
but I am still getting error undefined local variable or method `properties' for #<RSpec::ExampleGroups::PropertiesBuildController
0

I am a beginner so I think I got the incorrect understanding of Routing and controller tests. As it was still giving an error and after discussing with another programmer. I finally resolved using the following pattern.

get :new, property_id: property.id

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.