使用 shoulda 重构 Rails 模型上的 rspec 测试

2024-05-09

了解后应该匹配器 https://github.com/thoughtbot/shoulda-matchers通过回答关于属性可访问性测试的另一个 StackOverflow 问题 https://stackoverflow.com/a/11849608/567863(并且认为它们非常棒),我决定尝试重构我在中所做的模型测试Rails 教程 http://ruby.railstutorial.org/试图使它们更加简洁和彻底。我这样做得益于模块文档的一些灵感Shoulda::Matchers::ActiveRecord http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames and Shoulda::Matchers::ActiveModel http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames, 也这个 StackOverflow 答案 https://stackoverflow.com/a/4611017/567863关于在模型中构建shoulda测试。然而,仍然有一些事情我不确定,我想知道如何才能使这些测试变得更好。

我将使用 Rails 教程中的用户规范作为示例,因为它是最详细的,并且涵盖了许多可以改进的领域。下面的代码示例在原来的基础上进行了更改用户规范.rb https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/models/user_spec.rb,并向下替换代码,直到describe "micropost associations"线。该规范测试针对user.rb https://github.com/railstutorial/sample_app_2nd_ed/blob/master/app/models/user.rb模型,其工厂定义在工厂.rb https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/factories.rb.

规格/模型/user_spec.rb

# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  name            :string(255)
#  email           :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  password_digest :string(255)
#  remember_token  :string(255)
#  admin           :boolean          default(FALSE)
#
# Indexes
#
#  index_users_on_email           (email) UNIQUE
#  index_users_on_remember_token  (remember_token)
#

require 'spec_helper'

describe User do

  let(:user) { FactoryGirl.create(:user) }

  subject { user }

  describe "database schema" do
    it { should have_db_column(:id).of_type(:integer)
                              .with_options(null: false) }
    it { should have_db_column(:name).of_type(:string) }
    it { should have_db_column(:email).of_type(:string) }
    it { should have_db_column(:created_at).of_type(:datetime)
                              .with_options(null: false) }
    it { should have_db_column(:updated_at).of_type(:datetime)
                              .with_options(null: false) }
    it { should have_db_column(:password_digest).of_type(:string) }
    it { should have_db_column(:remember_token).of_type(:string) }
    it { should have_db_column(:admin).of_type(:boolean)
                              .with_options(default: false) }
    it { should have_db_index(:email).unique(true) }
    it { should have_db_index(:remember_token) }
  end

  describe "associations" do
    it { should have_many(:microposts).dependent(:destroy) }
    it { should have_many(:relationships).dependent(:destroy) }
    it { should have_many(:followed_users).through(:relationships) }
    it { should have_many(:reverse_relationships).class_name("Relationship")
                         .dependent(:destroy) }
    it { should have_many(:followers).through(:reverse_relationships) }
  end

  describe "model attributes" do
    it { should respond_to(:name) }
    it { should respond_to(:email) }
    it { should respond_to(:password_digest) }
    it { should respond_to(:remember_token) }
    it { should respond_to(:admin) }
    it { should respond_to(:microposts) }
    it { should respond_to(:relationships) }
    it { should respond_to(:followed_users) }
    it { should respond_to(:reverse_relationships) }
    it { should respond_to(:followers) }
  end

  describe "virtual attributes and methods from has_secure_password" do
    it { should respond_to(:password) }
    it { should respond_to(:password_confirmation) }
    it { should respond_to(:authenticate) }
  end

  describe "accessible attributes" do
    it { should_not allow_mass_assignment_of(:password_digest) }
    it { should_not allow_mass_assignment_of(:remember_token) }
    it { should_not allow_mass_assignment_of(:admin) }
  end

  describe "instance methods" do
    it { should respond_to(:feed) }
    it { should respond_to(:following?) }
    it { should respond_to(:follow!) }
    it { should respond_to(:unfollow!) }
  end

  describe "initial state" do
    it { should be_valid }
    it { should_not be_admin }
    its(:remember_token) { should_not be_blank }
    its(:email) { should_not =~ /\p{Upper}/ }
  end

  describe "validations" do
    context "for name" do
      it { should validate_presence_of(:name) }
      it { should_not allow_value(" ").for(:name) }
      it { should ensure_length_of(:name).is_at_most(50) }
    end

    context "for email" do
      it { should validate_presence_of(:email) }
      it { should_not allow_value(" ").for(:email) }
      it { should validate_uniqueness_of(:email).case_insensitive }

      context "when email format is invalid" do
        addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
        addresses.each do |invalid_address|
          it { should_not allow_value(invalid_address).for(:email) }
        end
      end

      context "when email format is valid" do
        addresses = %w[[email protected] /cdn-cgi/l/email-protection [email protected] /cdn-cgi/l/email-protection [email protected] /cdn-cgi/l/email-protection [email protected] /cdn-cgi/l/email-protection]
        addresses.each do |valid_address|
          it { should allow_value(valid_address).for(:email) }
        end
      end
    end

    context "for password" do
      it { should ensure_length_of(:password).is_at_least(6) }
      it { should_not allow_value(" ").for(:password) }

      context "when password doesn't match confirmation" do
        it { should_not allow_value("mismatch").for(:password) }
      end
    end

    context "for password_confirmation" do
      it { should validate_presence_of(:password_confirmation) }
    end
  end

  # ...
end

关于这些测试的一些具体问题:

  1. 是否值得测试数据库模式?中的一条评论上面提到的 StackOverflow 答案 https://stackoverflow.com/a/4611017/567863说“我只测试与行为相关的东西,我不考虑列或索引行为的存在。数据库列不会消失,除非有人故意删除它们,但你可以通过代码审查和信任”,我同意这一点,但是是否有任何有效的理由来测试数据库模式的结构,从而证明存在的合理性Shoulda::Matchers::ActiveRecord模块?也许只有重要的指标才值得测试……?
  2. Do the should have_many测试下"associations"替换它们对应的should respond_to测试下"model attributes"?我无法判断是否should have_many测试只是寻找相关的has_many模型文件中的声明或实际上执行与以下相同的功能should respond_to.
  3. 您还有其他意见/建议可以使这些测试在内容和结构上更加简洁/可读/彻底吗?

1) Shoulda::Matchers::ActiveRecord 模块不仅仅包含列和索引匹配器。我会在周围挖掘包含的课程 https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_record一点点,看看你能找到什么。这就是have_many, belong_to等来自。但郑重声明,我认为其中大部分内容没有什么价值。

2)是的,宏例如have_many测试的内容不仅仅是模型是否响应方法。来自源代码 https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_record/association_matcher.rb#L108-120,您可以准确地看到它正在测试什么:

def matches?(subject)
  @subject = subject
  association_exists? &&
    macro_correct? &&
    foreign_key_exists? &&
    through_association_valid? &&
    dependent_correct? &&
    class_name_correct? &&
    order_correct? &&
    conditions_correct? &&
    join_table_exists? &&
    validate_correct?
end

3)使测试更具可读性和/或更简洁绝对是一个需要回答的主观问题。每个人都会根据他们的背景和经验给你不同的答案。我个人会摆脱所有respond_to测试并用有价值的测试替换它们。当有人查看您的测试时,他们应该能够理解该类的公共 API。当我看到你的对象对“以下?”之类的内容做出响应时,我可以做出假设,但并不真正知道它的含义。这需要争论吗?它返回一个布尔值吗?是物体跟随某物,还是某物跟随物体?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 shoulda 重构 Rails 模型上的 rspec 测试 的相关文章

  • Rails:验证字符串的最小和最大长度,但允许其为空白

    我有一个想要验证的字段 我希望该字段能够留空 但如果用户输入数据 我希望它采用某种格式 目前我在模型中使用以下验证 但这不允许用户将其留空 validates length of foo maximum gt 5 validates len
  • 如何让 Sublime Text 2 与 Ruby on Rails 配合使用?

    我是 Ruby on Rails 的新手 这是我到目前为止所做的 我已经安装了 XCode Homebrew RVM Ruby 和 Rails 现在如何让 Sublime Text 2 与 Ruby on Rails 一起使用 你应该做的第
  • 来自 csv.read 模拟文件的 rspec 测试结果

    我正在使用 ruby 1 9 并且正在尝试执行 BDD 我的第一个测试 应该在 csv 中读取 有效 但第二个测试 我需要模拟文件对象 却不起作用 这是我的型号规格 require spec helper describe Person d
  • 将 R 与 Rsruby 集成

    我想知道是否有人有将 R 集成到 Rails 中的经验 特别是在 heroku 上 我熟悉 rsruby gem 它是 ruby 与 R 事实上的 也许是唯一的 绑定 但是有关将 R 与 Rails 集成的文档即使不是不存在 也是很少的 比
  • $ bundle exec rake db:reset 命令提升无法删除 db/development.sqlite3

    我试着跑 bundle exec rake db reset并在控制台上发现以下内容 Couldn t drop db development sqlite3
  • PostgreSQL安装错误——无法分配内存

    我正在尝试从 sqlite3 切换到 PostgreSQL 以在 Rails 中进行开发 这样我就不会遇到任何 heroku 问题 我遵循了heroku和链接到的Railscast上给出的建议 但是在brew安装postgresql后遇到了
  • Cucumber 是否不需要编写单元测试?

    我对 Ruby ROR 可用的测试框架数量之多感到有点困惑 我最近看了黄瓜轨道广播 http railscasts com episodes search cucumber并发现它们非常有趣 所以我开始玩游戏 然后努力从概念上考虑在哪里进行
  • 设计重定向到成功登录似乎在第一次登录尝试时卡住

    我正在使用 Ruby on Rails 开发一个小型应用程序 并使用 Devise 进行身份验证 我有两个登录系统设置 一个是user另一个是employee 当使用正确的电子邮件和密码登录时 设计会发送正确的重定向到返回路径 但它会卡在那
  • 载波无法删除图像

    我现在使用 Carrierwave 上传图像 一切都很好 除了一个 当我添加用于删除上传图像的复选框时 我收到错误 无法批量分配受保护的属性 remove image Form Model class Manufacturer lt Act
  • Rails 多租户架构,限制多个租户的访问范围

    目前我们有一个单租户数据库架构 MySQL 运行着超过 100 个数据库 我们使用 Apartment gem 切换子域上的数据库连接 一切都很顺利 然而 我们现在需要创建所谓的 伞 客户端 它可以访问一组现有客户端的所有数据 我不认为这对
  • Rails 4 - 带有 dependent-fields-rails 的条件 JS

    我正在尝试弄清楚如何在我的 Rails 4 应用程序中使用 dependent fields rails gem 我迷路了 我已将 underscore js 包含在我的供应商 javascripts 文件夹中 并更新了我的 applica
  • 在 Rails 5.1 及更高版本中,使用什么来代替“render :text”(和“render Nothing: true”)?

    轨道 5 1 其中就有老朋友render text 当你需要渲染的时候它非常有用some文本 但不想要视图模板的开销 例子 render text ok render text t business rules project access
  • 将文件传递给活动作业/后台作业

    我通过标准文件输入接收请求参数中的文件 def create file params file upload Upload create file file filename img png end 但是 对于大型上传 我想在后台作业中执行
  • A has_many Bs 其中 B 没有主键

    我有型号 A 和 B A has many B 并且 B 属于 A 到目前为止 一切都很好 除了我指定 B 没有主键 我不打算修改或删除单个 B 行 并且我预计会有数百万到数十亿的 B 行 因此省略主键将非常方便 节省空间 创建 B 表的迁
  • 如何使用 AngularJS、Devise 和 UI Router 全局实现身份验证?

    我对 Angular 很陌生 所以这可能是一个新手问题 我正在尝试实现一个简单的任务管理器 只是一个练习 以 Rails 作为后端 以 Angular 作为前端 到目前为止 我遵循了教程 一切正常 现在我想在全球范围内实施身份验证 这意味着
  • 使用 qunit 测试 emberjs/rails/devise 应用程序时“无法验证 CSRF 令牌真实性”

    我有一个 emberjs rails 应用程序 使用设备进行用户身份验证 我正在使用 qunit 构建集成测试 我在运行测试之前手动登录应用程序 测试期间可以处理 GET 请求 但 POST 请求失败 特别是 在发出 POST 请求时 我收
  • Rails 7 缺失部分

    我正在升级到 Rails 7 1 并在使用 JS 部分的视图中遇到奇怪的错误 缺少部分 account stripe js erb application stripe js erb 与 locale gt fr formats gt ht
  • 多租户 Rails 应用:不同技术的优缺点是什么?

    我最初是为一位客户编写 Ruby on Rails 应用程序的 现在 我正在更改它 以便它可以用于不同的客户 我的最终目标是某些用户 不是我 可以单击按钮并创建一个新项目 然后生成所有必要的更改 新架构 新表 代码处理 无需任何人需要我编辑
  • 将带撇号的字符串传递给辅助方法无法正确显示

    我正在使用 Rails 教程中的辅助方法 它将两个字符串连接在一起 以便在视图中的标题选择器中使用 它工作得很好 除非字符串中有撇号 当 group name 包含撇号时 结果如下 这是方法 app helpers application
  • Ruby/Rails - 访问“查找”表而不对其进行建模?

    这是针对 Ruby on Rails 3 0 x 项目的 我有一个 查找 表 其中包含来自供应商的数据 当我从其他来源导入数据时 我想检查此表 加入 SKU 以获取额外数据 在我的应用程序中为该表创建模型对我来说似乎不合适 我的应用程序永远

随机推荐