【问题标题】:Rails Shoulda Matcher - Issue with testing Modularized ModelRails Shoulda Matcher - 测试模块化模型的问题
【发布时间】:2018-01-21 16:44:08
【问题描述】:

我正在使用 RSpec 和 Shoulda-Matchers 来测试我的模型。

我知道如何测试模型是否是这样的

class Headquarter < ApplicationRecord
  has_many :branches
end

class Branch < ApplicationRecord
  belongs_to :headquarter
end

我可以像这样写一个 shoulda-matcher:

RSpec.describe Headquarter, type: :model do
  it { should have_many(branches)}
end

RSpec.describe Branch, type: :model do
  it { should belong_to(:headquarter)}
end

我的问题

但是当我使用模块化模型时会出现问题,例如Company::HeadquarterCompany::Branch

我尝试使用此代码:

RSpec.describe Company::Headquarter, type: :model do
  it { should have_many(:company_branches)}
end

但它给了我一个错误,似乎它没有将模型识别为模块化。

Failure/Error: it { should have_many(:company_branches)}
   Expected Company::Headquarter to have a has_many association called company_branches (Company::Branch does not have a headquarter_id foreign key.)

当我期待 company_headquarter_id时,请注意错误中的headquarter_id。这就是它无法识别 ID 的原因。

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    戳代码文档后,发现两个有用的sn-p:

    对于自定义类名

      # ##### class_name
      #
      # Use `class_name` to test usage of the `:class_name` option. This
      # asserts that the model you're referring to actually exists.
      #
      #     class Person < ActiveRecord::Base
      #       has_many :hopes, class_name: 'Dream'
      #     end
      #
      #     # RSpec
      #     RSpec.describe Person, type: :model do
      #       it { should have_many(:hopes).class_name('Dream') }
      #     end
      #
      #     # Minitest (Shoulda)
      #     class PersonTest < ActiveSupport::TestCase
      #       should have_many(:hopes).class_name('Dream')
      #     end
    

    对于自定义外键:

      # ##### with_foreign_key
      #
      # Use `with_foreign_key` to test usage of the `:foreign_key` option.
      #
      #     class Person < ActiveRecord::Base
      #       has_many :worries, foreign_key: 'worrier_id'
      #     end
      #
      #     # RSpec
      #     RSpec.describe Person, type: :model do
      #       it { should have_many(:worries).with_foreign_key('worrier_id') }
      #     end
      #
      #     # Minitest (Shoulda)
      #     class PersonTest < ActiveSupport::TestCase
      #       should have_many(:worries).with_foreign_key('worrier_id')
      #     end
    

    如何解决

    型号

    如果您有上述型号:

    Company::Headquarter,添加foreign_key标签

    class Company::Headquarter < ApplicationRecord
      has_many :company_branches, :class_name => 'Company::Branch', foreign_key: 'company_headquarter_id'
    
    end
    

    Company::Branch

    class Company::Branch < ApplicationRecord
      belongs_to :company_headquarter, :class_name => 'Company::Headquarter'
    end
    

    对于 RSpec

    headquarter_spec.rb,注意 class_namewith_foreign_key 函数。

    RSpec.describe Company::Headquarter, type: :model do
      it { should have_many(:company_branches).class_name('Company::Branch').with_foreign_key('company_headquarter_id')}
    end
    

    来源:Shoulda-Matchers, association matcher Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多