Ruby on rails RSpec集成测试-测试有多少锚定标签
谈到测试,我从未涉足过模型测试之外的领域,目前我正在学习如何创建自己的用户身份验证,而不是依赖Desive。自从我使用RSpec以来,已经有一段时间了,我不仅想对语法进行一次检查,而且想不出一种方法来确认当用户登录时,我的登录和注册确实正在消失 这是我的当前用户\u登录\u spec.rbRuby on rails RSpec集成测试-测试有多少锚定标签,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,谈到测试,我从未涉足过模型测试之外的领域,目前我正在学习如何创建自己的用户身份验证,而不是依赖Desive。自从我使用RSpec以来,已经有一段时间了,我不仅想对语法进行一次检查,而且想不出一种方法来确认当用户登录时,我的登录和注册确实正在消失 这是我的当前用户\u登录\u spec.rb require 'rails_helper' RSpec.describe "UsersLogins", type: :request do before(:each) do @user =
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\u路径时,RSpec在解释调用时有什么不同?我知道在Rails中如果我有
= link_to "Profile", current_user
它会自动将其解释为
= link_to "Profile", user_path(current_user)
假设我的用户资源位于routes.rb中
如果有人能为Rspec和Capybara推荐一些很好的集成和功能测试教程,那将非常棒,任何帮助/建议都将不胜感激。我试图将其作为集成测试而不是功能测试(据我所知,这些测试保存在requests目录中,可读性较差,因为它们不太像用户故事,但仍在检查站点的功能)
编辑:
所以我解决了部分问题。我安装了一个调试器,能够发现我的用户实际上没有正确登录
下面是我用来在工厂中解析密码的方法
user.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\u digest
,而是将其更改为password
和password\u 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_元素
在我的规格/支持范围内,我创建了一个模块
**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
我猜这是规范/请求测试中的测试所必需的。您应该记住,FactoryGirl.build不创建数据库实例。所以,若在测试之前并没有对用户表进行种子设定,那个么它可能是空的。
我建议您使用.create而不是.build。我在使用create和build之间来回切换。在使用build时,我甚至尝试在测试期间添加一个“假”id来模拟它。我在遵循Hartl的教程,并试图使用BCrypt的密码摘要,但对FactoryGirl的简单更改只是解决了我的部分问题。我向工厂添加了密码和密码确认,解决了没有正确用户的问题。我在编辑我的文章时遇到了第二个问题。我遇到的很多问题都是因为我在遵循Hartl的自定义身份验证教程,但是我想在minitest上使用RSpec和一个已经运行的应用程序的自定义视图。