Question

I've implemented the code below so I can update user attributes via strong parameters. It's straight out of devise's documentation and I understand it pretty well.

The problem however is that I'm really new to testing. I'm using SimpleCov and it tells me the u.permit bit is not covered. Unfortunately, I'm not quite sure how to test for this.

I'm using rspec and I'd like to know if anyone could point me in the right direction.

How do I test for this?

devise_parameter_sanitizer.for(:account_update) do |u|
  u.permit(:username, :email, :password, :password_confirmation)
end
Was it helpful?

Solution

Assuming you wanted to test this with a unit test and you have this code within a method definition, you could mock out the objects and stub some methods.

That said, I don't think unit tests are appropriate for this (nor does betterspec.org).

Since this code is reliant on a third-party library, I think this is where you should use an integration test. Therefore, I would approach this by testing the higher level concepts where this code is used; without knowing what that code does, I would assume something like account creation and account updating. Assuming those two scenarios are the instances when the code is used, I would create either feature or request tests for those two use cases.

Thoughtbot has a great blog post regarding feature tests using Rspec and Caypbara.

The Rspec relish documentation covers both feature specs and request specs.

If I were in your shoes, I'd skip the request specs and focus on the feature specs, but I'm a big fan of just unit tests and high level feature tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top