对 ActiveRecord 模型中包含的模块进行单元测试

发布于 2024-11-19 10:35:56 字数 411 浏览 7 评论 0原文

我有一个像这样的模块(但更复杂):

module Aliasable 
  def self.included(base)
    base.has_many :aliases, :as => :aliasable
  end
end

我将其包含在多个模型中。目前为了测试,我制作了另一个模块,如下所示,我只是将其包含在测试用例中

module AliasableTest 
  def self.included(base)
    base.class_exec do 
      should have_many(:aliases)
    end
  end
end

问题是如何单独测试该模块?或者上面的方法已经足够好了。似乎可能有更好的方法来做到这一点。

I have a module like this (but more complicated):

module Aliasable 
  def self.included(base)
    base.has_many :aliases, :as => :aliasable
  end
end

which I include in several models. Currently for testing I make another module as below, which I just include in the test case

module AliasableTest 
  def self.included(base)
    base.class_exec do 
      should have_many(:aliases)
    end
  end
end

The question is how do I go about testing this module in isolation? Or is the above way good enough. Seems like there is probably a better way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-11-26 10:35:56

首先,self.included 并不是描述模块的好方法,而 class_exec 则不必要地使事情变得复杂。相反,您应该扩展 ActiveSupport::Concern,如下所示:

module Phoneable
  extend ActiveSupport::Concern

  included do
    has_one :phone_number
    validates_uniqueness_of :phone_number
  end
end

您没有提及您正在使用的测试框架,但 RSpec 恰好涵盖了这种情况。试试这个:

shared_examples_for "a Phoneable" do
  it "should have a phone number" do
    subject.should respond_to :phone_number
  end
end

假设你的模型看起来像:

class Person              class Business
  include Phoneable         include Phoneable
end                       end

然后,在你的测试中,你可以这样做:

describe Person do
  it_behaves_like "a Phoneable"      # reuse Phoneable tests

  it "should have a full name" do
    subject.full_name.should == "Bob Smith"
  end
end

describe Business do
  it_behaves_like "a Phoneable"      # reuse Phoneable tests

  it "should have a ten-digit tax ID" do
    subject.tax_id.should == "123-456-7890"
  end
end

First off, self.included is not a good way to describe your modules, and class_exec is needlessly complicating things. Instead, you should extend ActiveSupport::Concern, as in:

module Phoneable
  extend ActiveSupport::Concern

  included do
    has_one :phone_number
    validates_uniqueness_of :phone_number
  end
end

You didn't mention what test framework you're using, but RSpec covers exactly this case. Try this:

shared_examples_for "a Phoneable" do
  it "should have a phone number" do
    subject.should respond_to :phone_number
  end
end

Assuming your models look like:

class Person              class Business
  include Phoneable         include Phoneable
end                       end

Then, in your tests, you can do:

describe Person do
  it_behaves_like "a Phoneable"      # reuse Phoneable tests

  it "should have a full name" do
    subject.full_name.should == "Bob Smith"
  end
end

describe Business do
  it_behaves_like "a Phoneable"      # reuse Phoneable tests

  it "should have a ten-digit tax ID" do
    subject.tax_id.should == "123-456-7890"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文