如何在test / fixtures / user.yml中创建一个可用于Devise 4集成测试的测试用户?

时间:2016-12-14 10:36:14

标签: ruby-on-rails devise

从下面的代码中可以看出,我已经在users.yml中创建了一个用户,其中包含了test_helper.rb中用于集成测试的设备帮助(这就是为什么我能够使用sign_in }方法 - 基于Devise's README),并在site_layout_test.rb中进行了传递集成测试。

我的问题是,当我尝试在email中评论用户john的encrypted_passwordcreated_atusers.yml参数时,我仍然有通过测试但我的新手心灵告诉我,我现在应该有一个失败的测试,因为用户john现在不是一个有效的用户。

这里发生了什么?为什么我还在考试?

顺便说一下,这是帮助我在users.yml

中创建用户的link
  

测试/装置/ users.yml里

john:
  name: John Rockefeller
  email: john@rockefeller.com
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
  created_at: <%= Time.zone.now %>
  

test_helper.rb中

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
  include ApplicationHelper
  include Devise::Test::IntegrationHelpers
end
  

测试/集成/ site_layout_test.rb

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:john)
  end

  test "layout links" do
    # Not signed in
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", new_user_session_path, count: 2
    assert_select "a[href=?]", new_user_registration_path
    get new_user_registration_path
    assert_template 'devise/registrations/new'
    assert_select "title", full_title("Sign up")
    get new_user_session_path
    assert_template 'devise/sessions/new'
    assert_select "title", full_title("Sign in")
    # Signed in
    sign_in @user
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", new_user_session_path, count: 0
    assert_select "a[href=?]", new_user_registration_path, count: 0
    assert_select "a[href=?]", edit_user_registration_path
    assert_select "a[href=?]", destroy_user_session_path
    get edit_user_registration_path
    assert_template 'devise/registrations/edit'
    assert_select "title", full_title(@user.name)
  end
end
编辑:解决了!我新下面的site_layout_test.rb

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:john)
  end

  test "non-signed in user layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", new_user_session_path, count: 2
    assert_select "a[href=?]", new_user_registration_path
  end

  test "signed in user layout links" do
    get new_user_session_path
    assert_template 'devise/sessions/new'
    post user_session_path, params: { user: { email: @user.email, password: 'password' } }
    assert_not flash.empty?
    assert_redirected_to root_path
    follow_redirect!
    assert_template 'static_pages/home'
    assert_select "a[href=?]", new_user_session_path, count: 0
    assert_select "a[href=?]", new_user_registration_path, count: 0
    assert_select "a[href=?]", edit_user_registration_path
    assert_select "a[href=?]", destroy_user_session_path
  end

  test "page title" do
    get new_user_registration_path
    assert_template 'devise/registrations/new'
    assert_select "title", full_title("Sign up")
    get new_user_session_path
    assert_template 'devise/sessions/new'
    assert_select "title", full_title("Sign in")
  end
end

1 个答案:

答案 0 :(得分:2)

您需要牢记两件事。

首先,灯具直接插入数据库。相应的模型实例化。因此,可以插入会使模型级验证失败的值。

其次,sign_in只是在会话中设置用户ID(您可以将其视为session[:user_id] = @user.id)。它执行 no 身份验证。在测试功能其他而非身份验证时(例如,在论坛上发布为用户),应该使用它。它会跳过与身份验证相关的整个请求 - 响应周期,让您直接进入要测试的控制器和操作。

为了测试身份验证,您应针对POST触发new_sessions_path请求(请替换您在应用中使用的路由名称):

post new_sessions_path, params: { email: '...', password: '...' }

最后但同样重要的是,我建议您将测试分成许多较小的测试。特别是你将它命名为layout links,但你测试登录会让人感到困惑。