5

我正在使用 minitest 与 factory girl 和 capybara 进行集成测试。当我不使用工厂女孩创建用户对象时,Capybara 工作正常,如下所示:

it "logs in a user successfully" do
    visit signup_path
    fill_in "Email", :with => "joey@ramones.com"
    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 => "joey@ramones.com"
    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

但是一旦我尝试用 factory girl 创建一个用户,奇怪的事情就开始发生了,比如 visit 方法和 click_button 方法停止工作。例如,这个测试似乎没有任何问题:

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

这是我的 factory.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

这是我得到的实际错误:

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

但是,visit login_path如果我删除,效果很好@user = FactoryGirl.create(:user)

这是Capybara的错误吗?还是我在这里做错了什么?

4

2 回答 2

2

我终于使用 DatabaseCleaner gem 使用DatabaseCleaner.strategy = truncation. 这就是我最终得到的结果:

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

工厂.rb

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

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

集成/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
于 2012-11-28T20:27:59.547 回答
-1

删除login_path并尝试使用url

例如

visit "/users/login"   #login_path
于 2012-11-27T08:20:07.943 回答