rails/rails-controller-testing

assigns converts nil to empty string

jstoebel opened this issue · 2 comments

I just made the switch to Rails 5.0. I often test state of my controllers by comparing to an expected hash like so:

expected_attrs = {"id"=>38,
  "name" => "some name"
  "comments"=>nil}

actual_attrs = assigns(:post).attributes
assert_equal expected_attrs, actual_attrs

This worked find in Rails 4.2. But after the switch the two hashes are not equal. Specifically, actual_attrs["comments"] is now "" not nil.

Is this an issue in this gem or a change in Rails 5? Or is there something else I'm misunderstanding? I'm happy to work on this issue if its fixable.

It is a change in 5.0. Now controller tests are parsing the attributes in the same way a browser does. There is no way a parameter get in the controller with nil if your request is using the HTML format, so now Rails is behaving in the same way of a browser.

gotcha, thanks so much!