0

sorry for my english, I have a problem while testing views with capybara. Capybara can't view the elements validated with cancan, I have this in my tests, to simulate that the user has permissions for all.

before(:each) do
  permission = FactoryGirl.create(:permission)
  role = FactoryGirl.build(:role)
  role.permissions << permission
  role.save
  user = FactoryGirl.create(:user, :role_id => role.id)
  sign_in user
end

and this in my views...

= link_to t('actions.new'), new_user_path if can?(:create, User)

But my tests don't pass

 Failure/Error: click_link t('actions.new')
 Capybara::ElementNotFound:
   no link with title, id or text 'Crear nuev@' found
 # (eval):2:in `click_link'
 # ./spec/views/users_spec.rb:53:in `block (3 levels) in <top (required)>'

I don't understand, my controllers hasn't problems.

Some help?

Thanks in advance, Regards...

el_quick
  • 4,656
  • 11
  • 45
  • 53
  • don't use link texts, use ids! – phoet Jun 29 '12 at 00:27
  • I used ids but doesn't work :/ I have noted that is not necesary assign permissions to current_user to test controllers, is as having permissions for everything, although not are specified (my controllers has restrictions, of course) – el_quick Jun 29 '12 at 14:14

1 Answers1

0

I've never tried using capybara with internationalization but I can imagine you might run into some issues.

I'd follow @phoet's advice and use an ID in your link instead of the translated link text. Capybara's click_link will look for either an ID or link text.

# add ID to link in view
= link_to(t('actions.new'), new_user_path, :id => 'actions_new') if can?(:create, User)

.

# in test file
click_link 'actions_new'
Dty
  • 12,253
  • 6
  • 43
  • 61