简体   繁体   English

Rails:如何知道单元测试中的测试模型?

[英]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.

相关问题 我怎么知道何时在Rails中“刷新”我的模型对象? - How can I know when to “refresh” my model object in Rails? 如何跳过Rails中的某些模型验证以加速单元测试 - How do I skip certain Model Validations in Rails for speeding up Unit tests 当我下载Rails开源项目时,我怎么知道需要哪个版本的Rails或gem? - When I download a Rails open source project, how can I know which version of Rails or gems is needed? 如何加快涉及图像上传/调整大小的Rails单元测试? - How can I speed up Rails unit tests involving image upload/resizing? 如何在Rails 3项目的CoffeeScript中编写Jasmine单元测试? - How can I write my Jasmine unit tests in CoffeeScript in my Rails 3 project? 如何确保在rails单元测试中正确更新counter_cache? - How can I ensure that counter_cache is updated correctly in rails unit tests? Rails单元测试使用附件的功能 - Rails unit tests for functions which use attachments 模型的 rails 单元测试返回无效结果 - rails unit tests for model returns invalid result 由于 model 回调,Rails 7 系统测试出现错误,我该如何解决? - Rails 7 system tests gives an error because of model callback, how can i fix this? 如何一次运行多个 Rails 单元测试 - How to run multiple Rails unit tests at once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM