0

When I test one of my spec's, I continue to receive this issue:

Failure/Error: visit "/"
UncaughtThrowError:
uncaught throw :warden

Below are the various sections of code:

#_spec.rb
require "rails_helper"

RSpec.feature "Users can create a new discussion" do
  before do
    login_as(FactoryGirl.create(:user))

    visit "/"
    click_link "Discussions"
    click_link "Start a discussion"
  end

  scenario "with valid attributes" do
    click_button "Post"
  end
end

#_factory.rb
FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "test#{n}@example.com" }
    password "password"
    sequence(:user_name) { |n| "User#{n}" }
    first_name "Test"
    last_name "User"
    country "United States"
    state "Nevada"
    city "Las Vegas"
  end
end

#rails_helper
config.include Warden::Test::Helpers, type: :feature
config.after(type: :feature) { Warden.test_reset! }

Obviously the test is flagging the visit line within the _spec file but I am unsure as to why. If there is any other code I could add to help, please let me know, thank you!

UPDATE

So the issue is because I am using confirmable with Devise; however, it is unclear to me how I could confirm a user through testing. Is this something I could place within the factory or the spec itself? I would think the spec itself?

Joe Dayvie
  • 304
  • 3
  • 14
  • Do you use Devise? What is `login_as`? – dimakura Sep 12 '15 at 21:34
  • 1
    Possible duplicate: http://stackoverflow.com/questions/9482739/uncaught-throw-warden-in-devise-testing. If you're using `Confirmable` module, you need to confirm your user, or Warden will throw an error. – Alexey Shein Sep 12 '15 at 21:35
  • Alex: I came across that last night but was unsure to if that was actually the issue, how I could confirm the user via testing. The whole Devise::TestHelpers confuses me. I just verified though and it is the confirmable aspect. I don't understand how I could confirm a user within the testing. Can I do that within the factory or must I mention within the feature itself? – Joe Dayvie Sep 12 '15 at 21:40

1 Answers1

1

Just issue a simple .confirm! on the user, i.e.:

before do
  user = FactoryGirl.create(:user)
  user.confirm!
  login_as(user)

  visit "/"
  click_link "Discussions"
  click_link "Start a discussion"
end

It seems you also lack setting warden to a test mode (put that into rails_helper):

RSpec.configure do |config|
  config.include Warden::Test::Helpers
  config.before :suite do
    Warden.test_mode!
  end
end

See other details here: https://github.com/plataformatec/devise/wiki/How-To%3a-Test-with-Capybara.

dimakura
  • 7,575
  • 17
  • 36
Alexey Shein
  • 7,342
  • 1
  • 25
  • 38
  • I have warden setup within my rails_helper, it is the last code within my question. Unless you are hinting that I have it setup incorrectly? Your suggestion for the confirmation worked though and makes complete sense. Was silly I did not realize that =P I thank you! – Joe Dayvie Sep 12 '15 at 22:01
  • It's just I don't see any line with `Warden.test_mode!` in your question. Have a look at the link in my answer, it seems you almost combined all pieces correctly. – Alexey Shein Sep 12 '15 at 22:04