简体   繁体   English

rails:如何使用gem提供模型,以及如何在没有虚拟app的情况下测试rails gem

[英]rails: how to supply a model with gem, and how to test rails gem without dummy app

I have already read The Basics Of Creating Rails Plugins and several other articles, but I can't find how to supply a model with a gem. 我已经阅读了创建Rails插件的基础知识和其他几篇文章,但我找不到如何提供带有gem的模型。

Say, I want to make a gem for tagging (yes, I know about acts_as_taggable_on gem, I need different functionality). 说,我想制作一个标记宝石(是的,我知道acts_as_taggable_on gem,我需要不同的功能)。 So, I want the model Tag to be bundled in the gem. 所以,我希望将模型Tag捆绑在gem中。 I found no tutorial explaining that. 我发现没有教程解释。

Of course I tried to reverse-engineer acts_as_taggable_on gem to understand how does it work, but it brought even more confusion: the tutorial I mentioned above says that I should have a dummy app in my gem, in order to test the gem. 当然,我试图对acts_as_taggable_on gem进行反向工程,以了解它是如何工作的,但它带来了更多的混乱:我上面提到的教程说我应该在我的gem中有一个虚拟应用程序,以便测试gem。 BUT, acts_as_taggable_on has no such dummy application! 但是,acts_as_taggable_on没有这样的虚拟应用程序! How how does it get tested, then? 那怎么测试呢?

About the model: ok, I see the file lib/acts_as_taggable_on/tag.rb that seems to be a Tag model: 关于模型:好的,我看到文件lib/acts_as_taggable_on/tag.rb似乎是一个Tag模型:

module ActsAsTaggableOn
  class Tag < ::ActiveRecord::Base
    # ..........................
  end
end

I see that file lib/acts-as-taggable-on.rb requires tag : 我看到文件lib/acts-as-taggable-on.rb需要tag

require "acts_as_taggable_on/tag"

So I've applied the same approach (assume my plugin is named dftags) : 所以我应用了相同的方法(假设我的插件名为dftags):

I have added file lib/dftags/tag.rb : 我添加了文件lib/dftags/tag.rb

module Dftags
  class Tag < ::ActiveRecord::Base
    # attr_accessible :title, :body
  end
end

And my lib/dftags.rb looks like this: 我的lib/dftags.rb看起来像这样:

module Dftags
end

require "dftags/tag"

I have specs tag_spec.rb : 我有specs tag_spec.rb

require 'spec_helper'

describe Tag do
  let(:tag) { Tag.new(name: "") }
  it { should validate_presence_of :name }
end

And when I run bundle exec rspec spec/ , I got error unitialized constant Tag (NameError) . 当我运行bundle exec rspec spec/ ,我得到了错误的unitialized constant Tag (NameError)

It seems I missed something important. 我似乎错过了重要的事情。 Plus, again, I have dummy app for testing, but acts_as_taggable_on doesn't; 另外,我还有用于测试的虚拟应用程序,但acts_as_taggable_on没有; so, the testing approach should be different.. 所以,测试方法应该是不同的..

So, the questions: 所以,问题:

  • How can I supply a model with gem? 我怎样才能提供带宝石的模型?
  • How can I test my gem without dummy app? 如何在没有虚拟应用的情况下测试我的宝石?
  • Are there some advanced docs about writing rails gems? 是否有一些关于编写rails gem的高级文档? Actually I tried to check out one more famous gem: devise , but the ruby-fu and rails-fu of the authors is too strong for me to understand it. 实际上我试图查看一个更有名的宝石: 装饰 ,但作者的ruby-fu和rails-fu太强大了,我无法理解它。 Where do people learn all of it? 人们在哪里学习所有这些?

How can I test my gem without dummy app? 如何在没有虚拟应用的情况下测试我的宝石?

The dummy app is only a helper that allows you to use your normal rails testing workflow when building a gem / plugin. 虚拟应用程序只是一个帮助程序,允许您在构建gem /插件时使用正常的rails测试工作流程。 You could run the tests without a dummy app but you would need a lot more manual work. 您可以在没有虚拟应用程序的情况下运行测试但是您需要更多的手动工作。

acts_as_taggable_on is pretty much active_record only with the exception of a single helper (as far as I can tell from a quick glance). acts_as_taggable_on几乎是active_record只有一个帮助器(我可以快速浏览一下)。 The author therefore decided that the overhead of maintaining the dummy app was not worth the effort and is setting up active_record by hand. 因此,作者认为维护虚拟应用程序的开销不值得付出努力,并且手动设置active_record See here https://github.com/mbleigh/acts-as-taggable-on/blob/master/spec/spec_helper.rb#L24 how he establishes the connection to the database. 请参阅https://github.com/mbleigh/acts-as-taggable-on/blob/master/spec/spec_helper.rb#L24他如何建立与数据库的连接。

This code would not be necessary when using a dummy app as rails is taking care of it. 使用虚拟应用程序时,此代码不是必需的,因为rails正在处理它。

The same is true for the helper. 助手也是如此。 Instead of using the test methods provided by rails he creates a new Class that includes the helper and uses an instance of this class to test it (h/acts-as-taggable-on/blob/master/spec/acts_as_taggable_on/tags_helper_spec.rb#L11). 他没有使用rails提供的测试方法,而是创建了一个包含帮助程序的新类,并使用该类的实例对其进行测试(h / acts-as-taggable-on / blob / master / spec / acts_as_taggable_on / tags_helper_spec.rb #L11)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM