Ruby-on-rails Rails RSpec 测试 has_many :through 关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3973745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:25:56  来源:igfitidea点击:

Rails RSpec Tests for a has_many :through Relationship

ruby-on-railsrubyrspechas-manyhas-many-through

提问by Mario Zigliotto

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

我是测试和 Rails 的新手,但我正在尝试正确地关闭我的 TDD 过程。

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

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

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:

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

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

What else am i missing? thanks!!

我还缺少什么?谢谢!!

采纳答案by randmin

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

为完整起见,2020 年无需额外宝石即可实现。

  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)

(参见Ruby API on Reflection

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

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

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

但是,它不限于 has_many :through 关系,还可以用于应用于模型的任何关联。

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

(注意:这是在Rails 5.2.4 中使用新的 rspec 2.11 语法

回答by Peter Brown

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

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

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

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

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

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

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

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

I find the Shoulda Rdocpage very helpful too.

我发现Shoulda Rdoc页面也很有帮助。

回答by GregC

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

回答by Wayne Conrad

remarkablewill 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.

通常,您只需将模型中的所有关系复制到规范中,然后将“has”更改为“should_have”,将“belongs_to”更改为“should_belong_to”,依此类推。为了回答它正在测试 Rails 的指控,它还检查数据库,确保关联有效。

Macros are also included for checking validations as well:

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

should_validate_numericality_of :amount, :greater_than_or_equal_to => 0