【问题标题】:Integration testing Devise using Test::Unit使用 Test::Unit 进行集成测试设计
【发布时间】:2015-05-27 19:29:29
【问题描述】:

我已安装 Devise 并将其集成到我的应用中,现在我正在构建它的测试套件。我正在使用 Rails 4.2.1 附带的默认测试套件(Test::Unit?),目前正在开发集成套件。

集成测试:

  def setup
    @user = User.create(email: "user@hlist.com", 
                        encrypted_password: Devise.bcrypt(User, 'password1'))
    @post = { title: "This is the title",
               content: "Detailed comment."*10,
               phone: 9991118888,
               email: "email@hlist.com",
               user_id: users(:spiderman).id }
    @p = @user.posts.build(@post)
  end

  test "creates a new post successfully" do 
    sign_in_as(@user)
    get new_post_path(@user)
    assert_template 'posts/new'
    assert_difference 'Post.count', 1 do 
      post posts_path, post: @post
    end
    assert_template 'posts/show'
  end

我还在 test_helper.rb 文件中创建了以下方法:

  def sign_in_as(user)
     post_via_redirect user_session_path, 'user[:email]' => user.email, 
                           'user[:encrypted_password]' => Devise.bcrypt(User, 'password1')
  end

但是,我在运行测试时收到以下错误:

  1) Failure:
PostsCrudTest#test_creates_a_new_post_successfully [/Users/harishramachandran/dropbox/documents/harish/coding/workspace/h_list/test/integration/posts_crud_test.rb:19]:
expecting <"posts/new"> but rendering with <[]>

我在网上寻找解决方案,但我能找到的只是涉及 RSpec 或 Capybara 的解决方案。这是我创建的一个应用程序,除其他外,在我在另一个应用程序中转到 RSpec 和 Capybara 之前学习默认套件。有没有办法在 Test::Unit 中解决这个问题?

【问题讨论】:

  • 我认为你不应该将@user 传递给get new_post_path(@user),因为get new_post_path 只是一个表单。创建帖子时,创建操作应负责创建与当前用户关联的帖子。试试get new_post_path
  • 我刚刚尝试过,它给了我同样的错误。
  • 是否可以将 Unit::Test 集成测试与设计一起使用?还是我应该坚持使用控制器和模型测试?

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


【解决方案1】:

post posts_path, post: @post 创建帖子后,您不会被重定向到帖子/节目。像这样输入follow_redirect! post posts_path, post: @post follow_redirect! 或者你也可以做post_via_redirect posts_path, post: @post,它会做同样的事情。发布然后重定向到发布/显示。

一些注意事项 Devise.bcrypt 在几天前已被弃用。因此,将来如果您更新 Gemfile,您将收到弃用错误。 是的,您可以使用 Rails 默认测试进行集成测试。这是一个测试用户是否在登录或未登录的情况下获取根路径的示例。

require 'test_helper'

class UserFlowsTest < ActionDispatch::IntegrationTest
  test "user can see home page after login" do
    get user_session_path
    assert_equal 200, status
    @david = User.create(email: "david@mail.com", password: Devise::Encryptor.digest(User, "helloworld"))
    post user_session_path, 'user[email]' => @david.email, 'user[password]' =>  @david.password
    follow_redirect!
    assert_equal 200, status
    assert_equal "/", path
  end

  test "user can not see home page without login" do
    get "/"
    assert_equal 302, status
    follow_redirect!
    assert_equal "/users/sign_in", path
    assert_equal 200, status
  end
end

用于测试的 Rails 类(ActiveSupport::TestCase、ActionController::TestCase、ActionMailer::TestCase、ActionView::TestCase 和 ActionDispatch::IntegrationTest)继承自 minitest assertions 而不是 test unit 的断言。这适用于您的 Rails 版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2011-12-08
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多