[英]Is there a Lint for ActiveRecord?
Rubyists, particularly Railsers, know all about ActiveModel::Lint , which is a handy way to ensure that you haven't cocked up when writing your ActiveModel subclass, and saves hours of head-scratching. Rubyists,尤其是Railsers,对ActiveModel :: Lint都了解,这是一种方便的方法,可确保您在编写ActiveModel子类时不会翘起,并节省了数小时的时间。 I use it all the time for table-less models in Rails projects.
我一直将它用于Rails项目中的无表模型。
But I spend a lot of time hunting down errors with my usage of ActiveRecord - particularly with double-sided associations. 但是我花了很多时间来寻找使用ActiveRecord的错误-尤其是双面关联。 For example
例如
# app/models/blog.rb
class Blog < ActiveRecord::Base
has_many :posts # so far so good...
end
# app/models/post.rb
class Post < ActiveRecord::Base
has_one :blog # incorrect - should be belongs_to
end
That example is somewhat contrived, as nobody's that silly. 这个例子是有点做作,因为没有人这么愚蠢的。 But what if
Blog
actually has a post_id
column? 但是,如果
Blog
实际上有一个post_id
列怎么办? Exceptions would not be raised where expected. 异常不会在预期的地方引发。
I'd like to be able to write a test for Blog
like this 我希望能够为
Blog
这样编写测试
# test/unit/blog_test.rb
require 'test/test_helper'
class BlogTest < ActiveSupport::TestCase
include ActiveRecord::Lint
end
...and the test output should say something like Blog :has_many :posts, but Post does not :belong_to :blog!
...并且测试输出应显示类似
Blog :has_many :posts, but Post does not :belong_to :blog!
. 。 It would need to reflect on the associations, dealing with table-names, foreign-keys, etc, and considering :through models on the way.
它将需要反思关联,处理表名,外键等,并考虑以下方式:通过模型。 I know that proper unit tests will detect these issues anyway, but generally as side-effects of other tests.
我知道适当的单元测试无论如何都会检测到这些问题,但通常会作为其他测试的副作用。
Does anyone know of a project which does this? 有人知道哪个项目吗? (I'm only interested in ActiveRecord >= 3.1).
(我只对ActiveRecord> = 3.1感兴趣)。 All I could find was active_record_lint , which doesn't really do this at all.
我所能找到的只是active_record_lint ,它根本没有做到这一点。
Additionally, it would be great if all the existing fixtures could be tested automatically, to make sure that those pesky associations are set up. 另外,如果可以自动测试所有现有的灯具,以确保设置了那些令人讨厌的关联,那就太好了。 I usually just do
我通常只是做
test "fixtures" do
Post.all.each { |p| assert p.valid? "Fixture is broken! #{p.inspect}" }
end
but there's surely a more elegant way. 但是肯定有一种更优雅的方式。
A tool like this would be useful, but you should be testing these things explicitly anyway. 这样的工具会很有用,但是无论如何您都应该明确地测试这些东西。 This will uncover problems like this immediately.
这将立即发现类似的问题。 For instance, a typical unit test:
例如,一个典型的单元测试:
def test_defaults
blog = Blog.create(
:title => 'Example Blog'
)
assert blog.valid?
assert_equal [ ], blog.errors.full_messages
assert !blog.new_record?
assert_equal 0, blog.posts.count
end
For your post: 对于您的帖子:
def test_defaults
blog = Blog.create(
:title => 'Example Blog'
)
post = Post.create(
:blog => blog,
:content => 'Blogging blog bloggers blogged blogs'
)
assert post.valid?
assert_equal [ ], post.errors.full_messages
assert !post.new_record?
assert_equal blog.id, post.blog_id
assert_equal 1, blog.posts.count
end
Those are the long form versions of some helper methods that I usually use. 这些是我通常使用的一些辅助方法的长格式版本。
If you don't exercise the relationships, you're not testing them and you've got a problem with your test coverage. 如果您不练习这些关系,则说明您没有测试它们,因此您的测试覆盖范围有问题。 If you exercise a bad relationship it will show up as an error.
如果您关系不好,它将显示为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.