Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara not working with factory girl

I'm using minitest with factory girl and capybara for integration tests. Capybara works fine when I don't user factory girl to create a user object, like this:

it "logs in a user successfully" do
    visit signup_path
    fill_in "Email", :with => "[email protected]"
    fill_in "Password", :with => "rockawaybeach"
    fill_in "Password confirmation", :with => "rockawaybeach"
    click_button "Create User"
    current_path == "/"
    page.text.must_include "Signed up!"
    visit login_path
    fill_in "Email", :with => "[email protected]"
    fill_in "Password", :with => "rockawaybeach"
    check "Remember me"
    click_button "Log in"
    current_path == "/dashboard"
    page.text.must_include "Logged in!"
    page.text.must_include "Your Dashboard"
  end

But as soon as I try to create a user with factory girl weird things start happening, such as the visit method and click_button methods stop working. For instance, there doesn't seem to be anything wrong with this test:

require "test_helper"

describe "Password resets" do
  before(:each) do
    @user = FactoryGirl.create(:user)
  end

  it "emails user when requesting password reset" do
    visit login_path
    click_link "password"
    fill_in "Email", :with => user.email
    click_button "Reset my password"
  end

end

And here's my factories.rb:

FactoryGirl.define do
  factory :user do |f|
    f.sequence(:email) { |n| "foo#{n}@example.com" }
    f.password "secret"
    f.password_confirmation "secret"
  end
end

Here's the actual error that I'm getting:

est_0001_emails user when requesting password reset      0:00:01.624 ERROR
        undefined local variable or method `login_path' for #<#<Class:0x007fc2db48d820>:0x007fc2df337e40>

But, visit login_path works fine if I remove @user = FactoryGirl.create(:user)

Is this a bug with Capybara? Or am I doing something wrong here?

like image 458
Lee McAlilly Avatar asked Nov 13 '22 20:11

Lee McAlilly


1 Answers

I finally got this to work using the DatabaseCleaner gem using DatabaseCleaner.strategy = truncation. Here's what I ended up with:

test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
require "active_support/testing/setup_and_teardown"

class IntegrationTest < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  register_spec_type(/integration$/, self)

  def last_email
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    ActionMailer::Base.deliveries = []
  end
end

class HelperTest < MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown
  include ActionView::TestCase::Behavior
  register_spec_type(/Helper$/, self)
end

Turn.config.format = :outline 

# Database cleaner.
DatabaseCleaner.strategy = :truncation

factories.rb

FactoryGirl.define do
  sequence :email do |n|
    "email#{n}@example.com"
  end

  factory :user do
    email
    password "secret"
    password_confirmation "secret"
  end
end

integration/login_integration_test.rb

require "test_helper"

describe "Login integration" do    
  it "shouldn't allow an invalid login" do
    visit login_path
    click_button "Log In"
    page.text.must_include "invalid"
  end

  it "should login a valid user" do
    DatabaseCleaner.clean
    user = FactoryGirl.create(:user)
    visit login_path
    within ".session" do
      fill_in "session_email", :with => user.email
      fill_in "session_password", :with => "secret"
    end
    click_button "Log In"
    page.text.must_include "Logged in!"
    save_and_open_page
  end
end
like image 94
Lee McAlilly Avatar answered Nov 15 '22 08:11

Lee McAlilly