[英]Rails: How can I know which is the tested model in unit tests?
I have a test helper with the following signature, in test_helper.rb
: 我在test_helper.rb
有一个具有以下签名的测试助手:
assert_resolve_mapping(model_name, attrs_ary_to_exclude = [])
The first parameter is a string representing the actual model to be asserted, ie: User, Customer, Invoice, etc.. The second is an array of attributes. 第一个参数是表示要声明的实际模型的字符串,即:用户,客户,发票等。第二个参数是属性数组。
So in my unit tests I have: 所以在我的单元测试中,我有:
require 'test_helper'
class CustomerUnitTest < ActiveSupport::TestCase
test "should resolve mapping" do
assert_resolve_mapping("Customer", ["created_at", "updated_at"])
end
end
Since I have to call the helper in various unit tests for various models, is there a way to avoid passing the first parameter, "Customer"
in this case, knowing which is the model being actually tested? 由于我必须在针对各种模型的各种单元测试中调用帮助程序,因此,是否有一种方法可以避免传递第一个参数"Customer"
在这种情况下),从而知道实际测试的是哪个模型?
I believe you can parse the model name out of the class name. 我相信您可以从类名称中解析模型名称。
def assert_resolve_mapping(attrs_ary_to_exclude = [])
model_name = self.class.to_s.sub(/UnitTest\Z/, "")
...
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.