One of the tests in a scaffold-generated RSpec controller spec fails, and it looks to me as if it must always fail by design, but of course it is surely supposed to succeed.
I develop a Rails 4 app with RSpec specs generated by rails g scaffold.
The controller spec for my SkillsController requires me to fill in a 'valid attributes' hash and an 'invalid attributes' hash for my model, which I did.
The tests all succeed except for "PUT update with invalid params re-render the 'edit' template":
1) SkillsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: expect(response).to render_template("edit")
expecting <"edit"> but rendering with <[]>
# ./spec/controllers/skills_controller_spec.rb:139:in `block (4 levels) in <top (required)>'
In the Rails console, I confirmed that my invalid_params hash contains invalid parameters ({ hack: 'hack' }).
The controller calls the skill_params method which returns an empty hash because my invalid_params hash contains only invalid parameters.
Calling skill.update(skill_params) with an empty skill_params hash returns true, so that the else part will never execute, and the 'new' template will not be rendered:
def update
respond_to do |format|
if @skill.update(skill_params) # empty hash due to invalid params!
format.html { redirect_to @skill, notice: 'Skill was successfully updated.' }
format.json { render :show, status: :ok, location: @skill }
else
format.html { render :edit }
format.json { render json: @skill.errors, status: :unprocessable_entity }
end
end
end
To summarize: The spec PUTs a hash with invalid parameters to my SkillController. The SkillController's 'skill_params' cleans up this hash, returning an empty hash. skill.update with an empty hash is a no-op (confirmed on the console), and the method returns true.
Therefore, the assertion that the 'edit' template should be rendered will never, ever be true, and the default controller spec for the update action with invalid params will never turn green.
What am I missing here?