mardi 30 juin 2015

Is it possible to test after_create method in integration test for rolify gem?

I am using the Rolify gem to give users roles. It seems to work succesfully and I have several tests that pass without problems. However there is one test that doesn't seem to work.

Rolify offers to add after_create, which I added to my User model:

after_create :assign_mod_role

def assign_mod_role
  self.add_role :moderator, self.organization
end

This assigns the moderator role to each user that is created (a user is always created with an association to an organization). This works in development. I have also created an integration test:

test "user signup" do
  log_in_as(@admin)    #@admin of @organization1
  assert_difference 'User.count' do
    post adduser_path, user: { email: "ppppp@pppp.pp",
                               username: "pppppp",
                               password: "pppppp",
                               password_confirmation: "pppppp" }
  end
  testuser = assigns(:user)
  assert_equal testuser.has_role?(:moderator, @organization1), true
end

The last line however fails. Am I doing something wrong or is it not possible to test for the after_create method? Perhaps the way Rolify manages roles does not allow to test for this?

Aucun commentaire:

Enregistrer un commentaire