【问题标题】:RSpec Integration Testing - Testing how many anchor tagsRSpec 集成测试 - 测试有多少锚标签
【发布时间】:2016-07-12 21:23:33
【问题描述】:

在测试方面,我从未涉足模型测试之外,我目前正在学习如何创建自己的用户身份验证,而不是依赖于 Devise。自从我使用 RSpec 以来已经有一段时间了,我不仅希望对语法进行一点完整性检查,而且我无法找到一种方法来确认我的登录和注册确实在用户消失时消失了登录。

这是我当前的 users_logins_spec.rb

require 'rails_helper'

RSpec.describe "UsersLogins", type: :request do

  before(:each) do
    @user = FactoryGirl.build(:user)
  end

  it "login with invalid information" do
    get login_path
    expect(response).to render_template(:new)
    post login_path, session: { email: "", password: "" }
    expect(response).to render_template(:new)
    expect(flash).to be_present
    get root_path
    expect(flash).not_to be_present
  end

  it "login with valid information" do
    get login_path
    post login_path, session: { email: @user.email, password: "password"}
    expect(response).to redirect_to(@user)
    follow_redirect!
    expect(response).to render_template('users/show')
    # expect(page).to have_selector('a', login_path)

  end
end

强调最后一个测试,因为那是失败的测试。我相信,如果我将 ID 放在我想要检查的标签上,我将能够用我理解的方法规避我遇到的问题。我的目的是学习如何操作我的测试,而不必找到在测试之外更改我的代码的变通办法,尽管这将是一个很小的变化。

另一个问题是处理重定向。当我想重定向到@user 的@user url_path 时,RSpec 在解释调用时有何不同?我知道在 Rails 中如果我有类似的东西

= link_to "Profile", current_user

它会自动将其解释为

= link_to "Profile", user_path(current_user)

假设我的用户资源在 routes.rb 中。

如果有人能推荐一些 Rspec 与 Capybara 进行集成和功能测试的优秀教程,那将是非常棒的,任何帮助/建议将不胜感激。我正在尝试将其作为集成测试而不是功能测试(据我了解,这些测试保存在请求目录中并且“可读性较差”,因为它们不像用户故事那样多,但仍在检查功能网站)

编辑:

所以我发现了部分问题。我安装了一个调试器,发现我的用户实际上并没有正确登录。

这是我在工厂内用来消化密码的方法。

用户.rb

  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

工厂.rb

FactoryGirl.define do
  factory :user do
    sequence(:id) { |n| n }
    sequence(:name) { |n| "foo#{n}" }
    email { "#{name}@example.com" }
    password_digest User.digest('password')
  end
end

问题似乎是我的用户登录凭据无效,我不确定原因。

最终编辑 - 已解决

好的,所以我开始工作了。我的用户不正确的问题很容易解决。我没有在工厂内使用password_digest,而是将其更改为passwordpassword_confirmation,并开始重定向。我最初有 FactoryGirl.create(user) 并且在整个测试过程中一直在两者之间切换,但为了让它与必须创建的确认一起工作。

下一个问题实际上是 assert_select。

这是错误:

NotImplementedError:
   Implementing document_root_element makes assert_select work without needing to specify an element to select from.

我最终找到了解决方案。显然这是最新版本的 RSpec,我发现的解决方案是设置 document_root_element。

在我的规范/支持中,我创建了一个模块

**spec/support/assert_select_root.rb    
module AssertSelectRoot
  def document_root_element
    html_document.root
  end
end

RSpec.configure do |config|
    config.include AssertSelectRoot, :type => :request
end

我猜这是规范/请求测试中的测试所必需的

【问题讨论】:

  • 我遇到的很多问题是因为我按照 Hartl 的自定义身份验证教程进行操作,但我想在 minitest 上使用 RSpec 并为已经运行的应用程序使用自定义视图。

标签: ruby-on-rails ruby rspec


【解决方案1】:

乔。您应该记住 FactoryGirl.build 不会创建数据库实例。因此,如果您在测试前没有播种,您的用户表可能是空的。 我建议你使用 .create 而不是 .build。

【讨论】:

  • 我在使用 create 和 build 之间来回切换。在使用 build 时,我什至尝试在测试期间添加一个“假”id 来模拟它。我正在关注 Hartl 的教程并尝试将 password_digest 与 BCrypt 一起使用,但对 FactoryGirl 的简单更改只是解决了我的部分问题。我在工厂添加了密码和密码确认,它解决了没有正确用户的问题。我正在用第二个问题编辑我的帖子。
猜你喜欢
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多