#StackBounty: #ruby-on-rails #parameters #controller #ruby-on-rails-5 #minitest In Rails and minitest, how do I name my parameters when…

Bounty: 50

I’m using Rails 5 and minitest. How do I submit parameters in minitest to simulate a POST request where my model gets created? I have this test

  test "do create" do
    person = people(:one)
    score = 10
    post ratings_url, params: {rating[person_id]: person.id, rating[score]: score}

    # Verify we got the proper response
    assert_response :success
  end

but the above results in the error

localhost:myapp davea$ rails test test/controllers/ratings_controller_test.rb
Running via Spring preloader in process 30344
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require': /Users/davea/Documents/workspace/myapp/test/controllers/ratings_controller_test.rb:14: syntax error, unexpected ':', expecting => (SyntaxError)
url, params: {rating[person_id]: person.id, rating[score]: score

The only reason I named the POST parameters “rating[person_id]” and “rating[score]” is because that’s what they are named when the form is rendered in Rails as HTML.

Edit: In response to the answer given, I tried this in my test

post ratings_url, params: { rating: { person_id: person.id, score: score} }

and the controller looks like

def save(params)
  rating = Rating.new(rating_params)
  rating.user = current_user
  respond_to do |format|

and the error I got was

Error:
RatingsControllerTest#test_do_create:
ActionController::UnknownFormat: ActionController::UnknownFormat

Complaining about the line

respond_to do |format|


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.