设计在rails集成测试中不能正常工作

时间:2012-01-03 06:07:04

标签: ruby-on-rails ruby-on-rails-3 devise integration-testing

所以首先,我使用Rails附带的内置集成测试套件,而不是黄瓜,webrat等。我想在切换到另一个测试套件之前掌握内置套件。

使用设计1.4.2和rails 3.1。

无论如何,我正在编写一个集成测试,模拟用户注册帐户,确认帐户,重新登录并填写表单。这是事情似乎向南的地方。从我所知道的,Devise有我的控制器创建操作的问题,并且由于设计的这些问题,current_user是零。这是一个片段:

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest

test "new signup and application" do

  go_to_signup_page
  create_new_account :user => {:email => "kjsdfkjksjdf@sdfsdf.com", :user_name => "dfdfdf",     
  :password => "dfdfdfdfdfdf", :password_confirmation => "dfdfdfdfdfdf"}
  confirm_account_creation
  go_to_signin_page
  log_in
  go_to_form
  submit_form
end

.....其他测试......

def go_to_form
  get "/applications/new"
  assert_response :success
  assert_template "applications/new"
  assert_equal 'Please fill out this application', flash[:notice]
end


def submit_form
  post "/applications", :application => {.....}
  assert_response :redirect
  assert_equal 'Application Successful ', flash[:notice]
end

在我的控制器中我有

  before_filter :authenticate_user!

根据我的判断,“def go_to_form”通过了。但是当我尝试在“def submit_form”中发帖时,它告诉我需要“请先登录或注册”,这是您在未登录时尝试访问某个功能时的典型设计错误。 我认为这个问题也导致current_user为零。

如果我删除了before_filter,我要发帖但是current_user仍然是NIL。

我无法解释这里发生了什么。除了这个CREATE动作之外,所有其他动作都以漂亮的颜色传递。

1 个答案:

答案 0 :(得分:1)

这是我使用FactoryGirl的工作代码,您可以成功登录这段代码,确保输入了确切的电子邮件和密码。但是在Devise gem中还有内置测试用例,请看一下。

  setup do
    @user = FactoryGirl.create(:user)
  end

  def do_login
    visit '/'
    click_link "Sign in"
    fill_in 'user_email', :with => 'xxx@xxx.com'
    fill_in 'user_password', :with => 'xxxx'
    click_on('sign_in')
  end

  test 'should go to section index' do
    do_login
    assert page.has_content?('Signed in successfully.')
  end

这是一家工厂:

FactoryGirl.define do
  factory :user do

    email "xxx@xxx.com"
    password "xxxx"
    password_confirmation "xxxx"
    :user_name "admin"
    confirmed_at "#{Time.now }"
  end
end