2
votes

I've been researching how to test my Ability and Users for a small test rails wiki i'm building and i'm having difficulty wrapping my brain around how to test either or both ?.

I think i'd like to sign_in a user as admin/editor etc. and test that user behaves like it should. I'd like to also test that Pages are displaying the correct information for an authenticated user.

This is more then one problem and i'm looking for some basic nudging less then an answer :).

I use Rspec, Cancancan, Rails 4 and devise for authentication. I've read up on a few ways none of which worked for me:

  1. Cancan example
  2. Another ex i tried for cancan
  3. Devise and rails 4 I'm using route authentication for devise.
  4. Devise and sign_in

In any case here are my files. I'll just link to my git repo

  1. my abilities.rb
  2. pages.rb
  3. users.rb
  4. pages_controller.rb

I have factories as well but what's the trick to doing the following

  1. Signing in users: The examples suggest using the devise test helpers then creating a signed_in user stub. I haven't quite succeeded at this yet :( so a nudge in the right direction
  2. Devise and abilities: My understanding is I need to use the cancan/matchers then make some factories for admin/editor or any views and then do my usual tests once i've logged in a user and instantiated abilities. also failed at all the examples I tried.
  3. Finally testing the controller for pages: I required again logging in the user with the correct ability and then testing each action for each users to see if i received the correct records back?

Maybe i'm not separating out the roles enough or i'm missing some important steps from what I could read but even a small nudge on any of these would be much appreciated.

1

1 Answers

0
votes

You may have seen this, but this CanCan page https://github.com/ryanb/cancan/wiki/Testing-Abilities describes some basic high-level methodology behind testing the ability class. TL;DR Focus heavily on testing the Ability class itself and keep the controller tests light.