测试使用ActiveRecord的关注点/模块 [英] Testing a concern / module that uses ActiveRecord
问题描述
场景
我提取了一个名为Taggable
的关注点.它是一个模块,允许任何模型支持标记.我已将此关注点/模块包含在User
,Location
,Places
,Projects
等模型中.
SCENARIO
I have extracted a concern called Taggable
. It's a module that allows any model to support tagging. I have included this concern/module into models like User
, Location
, Places
, Projects
.
我想为此模块编写测试,但不知道从哪里开始.
I want to write tests for this module, but don't know where to start.
问题
1.我可以对Taggable
问题进行隔离测试吗?
在下面的示例中,测试失败,因为测试正在寻找dummy_class table
.由于Taggable
中的has_many
代码,我假设它正在执行此操作,因此它期望'DummyClass'
是ActiveRecord对象.
QUESTION
1. Can I do isolation testing on the Taggable
concern?
In the example below the test fails because the test is looking for a dummy_class table
. I'm assuming it's doing this because of the has_many
code in Taggable
so as a result it expects 'DummyClass'
to be an ActiveRecord object.
# /app/models/concerns/taggable.rb
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, :as => :taggable, :dependent=> :destroy
has_many :tags, :through => :taggings
end
def tag(name)
name.strip!
tag = Tag.find_or_create_by_name(name)
self.taggings.find_or_create_by_tag_id(tag.id)
end
end
# /test/models/concerns/taggable_test.rb
require 'test_helpers'
class DummyClass
end
describe Taggable do
before do
@dummy = DummyClass.new
@dummy.extend(Taggable)
end
it "gets all tags" do
@dummy.tag("dummy tag")
@dummy.tags.must_be_instance_of Array
end
end
我的一部分认为,如果我只是测试一个包含此模块(例如User
)的模型,就足以进行测试.但是我一直在读,您应该单独测试模块.
Part of me thinks if I just test a model that has this module included inside of it like User
that's enough of a test. But I keep reading that you should test modules in isolation.
正在寻找有关正确方法的一些指导/策略.
Looking for some guidance / strategy on what the right approach is.
推荐答案
我建议让DummyClass
是一个通用的ActiveRecord::Base
子代,除了include Taggable
之外,它几乎没有自定义代码,这样您就可以隔离您的顾虑尽可能多的模块,但仍属于AR类.避免使用您的真实"类之一,例如User
,仍然可以将您与这些类中的任何其他代码隔离,这似乎很有价值.
I would suggest having DummyClass
be a generic ActiveRecord::Base
child with very little custom code besides just include Taggable
, so that you would be isolating your concern module as much as possible but still being an AR class. Avoiding the use of one of your "real" classes like User
still isolates you from any other code in those classes, which seems valuable.
是这样的:
class DummyClass < ActiveRecord::Base; end
describe Taggable do
before do
@dummy_class = DummyClass.new
end
...
end
由于您的DummyClass
可能实际上需要与数据库交互以测试诸如关联之类的事情,因此您可能需要在测试期间在数据库中创建临时表. temping Ruby gem可能会对此提供帮助,因为它旨在创建临时ActiveRecord模型及其底层数据库表.
Since your DummyClass
may need to actually interact with the DB to test things like associations, you may need to create temporary tables in the DB during testing. The temping Ruby gem may be able to help with that, since its designed to create temporary ActiveRecord models and their underlying database tables.
调优使您可以创建由临时SQL表支持的任意ActiveRecord模型,以用于测试.如果要测试要混入ActiveReord模型而无需中继具体类的模块,则可能需要执行以下操作.
Temping allows you to create arbitrary ActiveRecord models backed by a temporary SQL table for use in tests. You may need to do something like this if you're testing a module that is meant to be mixed into ActiveReord models without relaying on a concrete class.
这篇关于测试使用ActiveRecord的关注点/模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!