Rails RSpec 测试 has_many :through 关系

发布于 2024-09-28 15:50:38 字数 672 浏览 5 评论 0原文

我对测试和 Rails 很陌生,但我正在努力正确地完成我的 TDD 流程。

我想知道您是否使用任何类型的范例来测试 has_many :通过关系? (或者我想一般来说就是 has_many )。

例如,我发现在我的模型规范中,我肯定会编写简单的测试来检查关系两端的相关方法。

即:

require 'spec_helper'

describe Post do

  before(:each) do
    @attr = { :subject => "f00 Post Subject", :content => "8ar Post Body Content" }
  end

  describe "validations" do
  ...    
  end

  describe "categorized posts" do

    before(:each) do
      @post  = Post.create!(@attr)
    end

    it "should have a categories method" do
      @post.should respond_to(:categories)
    end

  end

end

然后在我的类别规范中,我进行反向测试并检查 @category.posts

我还缺少什么?谢谢!!

I'm new to testing and rails but i'm trying to get my TDD process down properly.

I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose).

For example, i find that in my model specs i'm definitely writing simple tests to check both ends of a relationship for relating methods.

ie:

require 'spec_helper'

describe Post do

  before(:each) do
    @attr = { :subject => "f00 Post Subject", :content => "8ar Post Body Content" }
  end

  describe "validations" do
  ...    
  end

  describe "categorized posts" do

    before(:each) do
      @post  = Post.create!(@attr)
    end

    it "should have a categories method" do
      @post.should respond_to(:categories)
    end

  end

end

Then in my categories spec i do the inverse test and check for @category.posts

What else am i missing? thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新雨望断虹 2024-10-05 15:50:38

我建议您查看一个名为 Shoulda 的 gem。它有很多宏用于测试关系和验证等内容。

如果您想要的只是测试 has_many 关系是否存在,那么您可以执行以下操作:

describe Post do
  it { should have_many(:categories) }
end

或者如果您正在测试 has_many :through,那么您可以使用这个:

describe Post do
  it { should have_many(:categories).through(:other_model) }
end

我找到 Shoulda Rdoc 页面也非常有帮助。

I would recommend checking out a gem called Shoulda. It has a lot of macros for testing things like relationships and validations.

If all you want is to test that the has_many relationship exists, then you could do the following:

describe Post do
  it { should have_many(:categories) }
end

Or if you're testing a has_many :through, then you'd use this:

describe Post do
  it { should have_many(:categories).through(:other_model) }
end

I find the Shoulda Rdoc page very helpful too.

靖瑶 2024-10-05 15:50:38

为了完整起见,在 2020 年,无需额外宝石即可实现此目的。

  it "has many categories" do
    should respond_to(:categories)
  end

甚至更明确:(

it "belongs to category" do
  t = Post.reflect_on_association(:category)
  expect(t.macro).to eq(:belongs_to)
end

请参阅 反射上的 Ruby API

第二个示例确保“has_one”不是
与“belongs_to”混淆,反之亦然。

但是,它不限于 has_many :通过关系,而是可用于应用于模型的任何关联。

(注意:这是使用新的 rspec 2.11 语法轨道 5.2.4)

For the sake of completeness, in 2020 this is possible without additional gems.

  it "has many categories" do
    should respond_to(:categories)
  end

And even more explicit:

it "belongs to category" do
  t = Post.reflect_on_association(:category)
  expect(t.macro).to eq(:belongs_to)
end

(see Ruby API on Reflection)

The second example makes sure that a "has_one" is not
confused with a "belongs_to" and vice versa

It is, however, not limited to has_many :through relationships, but can be used for any association applied to the model.

(Note: This is using the new rspec 2.11 syntax with Rails 5.2.4)

一桥轻雨一伞开 2024-10-05 15:50:38

remarkable 可以很好地做到这一点:

describe Pricing do

  should_have_many :accounts, :through => :account_pricings
  should_have_many :account_pricings
  should_have_many :job_profiles, :through => :job_profile_pricings
  should_have_many :job_profile_pricings

end

通常,您只需将所有关系从模型复制到规范即可并将“has”更改为“should_have”,将“belongs_to”更改为“should_belong_to”,依此类推。为了回应其正在测试轨道的指控,它还检查数据库,以确保关联有效。

还包含用于检查验证的宏:

should_validate_numericality_of :amount, :greater_than_or_equal_to => 0

remarkable will do this nicely:

describe Pricing do

  should_have_many :accounts, :through => :account_pricings
  should_have_many :account_pricings
  should_have_many :job_profiles, :through => :job_profile_pricings
  should_have_many :job_profile_pricings

end

Generally, you just copy all of your relationships from the model to the spec and change "has" to "should_have", "belongs_to" to "should_belong_to", and so on. To answer the charge that it's testing rails, it checks the database also, making sure that the association works.

Macros are also included for checking validations as well:

should_validate_numericality_of :amount, :greater_than_or_equal_to => 0
彻夜缠绵 2024-10-05 15:50:38
describe "when Book.new is called" do
  before(:each) do
    @book = Book.new
  end

  #otm
  it "should be ok with an associated publisher" do
    @book.publisher = Publisher.new
    @book.should have(:no).errors_on(:publisher)
  end

  it "should have an associated publisher" do
    @book.should have_at_least(1).error_on(:publisher)
  end

  #mtm
  it "should be ok with at least one associated author" do
    @book.authors.build
    @book.should have(:no).errors_on(:authors)
  end

  it "should have at least one associated author" do
    @book.should have_at_least(1).error_on(:authors)
  end

end
describe "when Book.new is called" do
  before(:each) do
    @book = Book.new
  end

  #otm
  it "should be ok with an associated publisher" do
    @book.publisher = Publisher.new
    @book.should have(:no).errors_on(:publisher)
  end

  it "should have an associated publisher" do
    @book.should have_at_least(1).error_on(:publisher)
  end

  #mtm
  it "should be ok with at least one associated author" do
    @book.authors.build
    @book.should have(:no).errors_on(:authors)
  end

  it "should have at least one associated author" do
    @book.should have_at_least(1).error_on(:authors)
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文